Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(view): allow function for view.float.open_win_config #1538

Merged
merged 1 commit into from
Aug 23, 2022

Conversation

alex-courtis
Copy link
Member

resolves #1512

@alex-courtis
Copy link
Member Author

alex-courtis commented Aug 22, 2022

@davidsierradz
Copy link
Contributor

Thanks! Tested and it works! Something that caught me off-guard is that I also need to define view.width and view.height to be functions, maybe add this to view.float docs?

This is what I have, for reference:

...
  view = {
    float = {
      enable = true,
      open_win_config = function()
        local screen_w = vim.opt.columns:get()
        local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
        local _width = screen_w * 0.7
        local _height = screen_h * 0.5
        local width = math.floor(_width)
        local height = math.floor(_height)
        local center_y = ((vim.opt.lines:get() - _height) / 2) - vim.opt.cmdheight:get()
        local center_x = (screen_w - _width) / 2
        local layouts = {
          center = {
            anchor = 'NW',
            relative = 'editor',
            border = 'single',
            row = center_y,
            col = center_x,
            width = width,
            height = height,
          },
        }
        return layouts.center
      end,
    },
    width = function()
      return math.floor(vim.opt.columns:get() * 0.7)
    end,
    height = function()
      return math.floor((vim.opt.lines:get() - vim.opt.cmdheight:get()) * 0.5)
    end,
  }
...

@alex-courtis
Copy link
Member Author

Something that caught me off-guard is that I also need to define view.width and view.height to be functions, maybe add this to view.float docs?

Interesting. Do view.width/height override open_win_config? Does that override occur if you set open_win_config to be a table?

If that's the case we should probably ignore view.width/height when floating. That will require deeper digging, as there are some other cases to consider, such as adaptive_width.

@davidsierradz
Copy link
Contributor

davidsierradz commented Aug 23, 2022

Interesting. Do view.width/height override open_win_config? Does that override occur if you set open_win_config to be a table?

Testing more, the problem seems to be only with view.width, for example:

require('nvim-tree').setup({
  view = {
    float = {
      enable = true,
      open_win_config = {
        relative = 'editor',
        height = 3,
        width = 3,
      },
    },
    width = 30,
    height = 30,
  },
})

Opens a float window with height=3 and width=30. Even if I remove view.width, float window opens with width=30 (so I guess is the default?)

This opens with height=100 and width=100:

require('nvim-tree').setup({
  view = {
    float = {
      enable = true,
      open_win_config = {
        relative = 'editor',
      },
    },
    width = 100,
    height = 100,
  },
})

@alex-courtis
Copy link
Member Author

Testing more, the problem seems to be only with view.width, for example:

Thank you. Raised #1543 with your workaround documented.

Almo7aya pushed a commit to Almo7aya/nvim-tree.lua that referenced this pull request Oct 11, 2022
@kryzar
Copy link

kryzar commented Dec 1, 2022

Hi.

I hope this is the right place to ask this question; please tell me otherwise.

I used the code given in the comments to center my floating window.

nvim-tree-screen

require('nvim-tree').setup({
    hijack_cursor = true,
    actions = {
        open_file = {
            quit_on_open = true,
            window_picker = {
                enable = true,
            }
        }
    },
    diagnostics = {
        enable = true,
        show_on_dirs = true
    },
    filters = {
        dotfiles = true
    },
    renderer = {
        full_name = true,
        highlight_git = true,
        indent_width = 4,
    },
    tab = {
        sync = {
            open = true
        }
    },
    view = {
        float = {
            enable = true,
            -- Center the window
            -- Thanks https://github.com/nvim-tree/nvim-tree.lua/pull/1538
            open_win_config = function()
                local screen_w = vim.opt.columns:get()
                local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
                local _height = screen_h * 0.8
                local _width = screen_w * 0.8
                local width = math.floor(_width)
                local height = math.floor(_height)
                local center_y = ((vim.opt.lines:get() - _height) / 2)
                                 - vim.opt.cmdheight:get()
                local center_x = (screen_w - _width) / 2
                return {
                        anchor = 'NW',
                        relative = 'editor',
                        border = 'single',
                        row = center_y,
                        col = center_x,
                        width = width,
                        height = height,
                    }
                end,
        },
        width = function()
          return math.floor(vim.opt.columns:get() * 0.7)
        end,
        height = function()
          return math.floor((vim.opt.lines:get() - vim.opt.cmdheight:get()) * 0.5)
        end,
        adaptive_size = true,
        mappings = {
            custom_only = false,
            list = {
                { 
                    key = "l",
                    action = "edit",
                    action_cb = edit_or_open },
                { 
                    key = "L",
                    action = "vsplit_preview",
                    action_cb = vsplit_preview },
                { 
                    key = "h",
                    action = "close_node" },
                {
                    key = "H",
                    action = "collapse_all",
                    action_cb = collapse_all }
            }
        },
    },
})

Unfortunately, borders lost their radius. How can I add the original radius? Or even better, is there or will be a more automatic, better way to center the floating window?

Thanks a lot for this beautiful work.

@alex-courtis
Copy link
Member Author

alex-courtis commented Dec 3, 2022

I hope this is the right place to ask this question; please tell me otherwise.

No worries, we can discuss here. We can open an issue later if we'r looking at making changes.

That's quite a nice solution. Perhaps you might add it to the wiki Recipes for others to use.

Unfortunately, borders lost their radius. How can I add the original radius? Or even better, is there or will be a more automatic, better way to center the floating window?

Dumb question: are the borders losing their radius due to border = 'single'? We are defaulting them to rounded...

@gegoune gegoune deleted the 1512-open_win_config-function branch December 3, 2022 06:20
@kryzar
Copy link

kryzar commented Dec 3, 2022

That's quite a nice solution. Perhaps you might add it to the wiki Recipes for others to use.

Sure, I'm happy to do it! I added a minor change to the code, deleting the
view.height attribute, as it lead to a warning at startup when the window was
narrow. As this key is not in the docs, I figures I'd just delete it.

screen-height-bug

I also deleted view.float.open_win_config.anchor, which didn't seem to be
affecting the code nor being mentioned in the docs.

If those changes are right for you then I can write the recipe. The complete code is:

local HEIGHT_RATIO = 0.8
local WIDTH_RATIO = 0.5

require('nvim-tree').setup({
    view = {
        float = {
            enable = true,
            open_win_config = function()
                local screen_w = vim.opt.columns:get()
                local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
                local window_w = screen_w * WIDTH_RATIO
                local window_h = screen_h * HEIGHT_RATIO
                local window_w_int = math.floor(window_w)
                local window_h_int = math.floor(window_h)
                local center_x = (screen_w - window_w) / 2
                local center_y = ((vim.opt.lines:get() - window_h) / 2)
                                 - vim.opt.cmdheight:get()
                return {
                    border = 'rounded',
                    relative = 'editor',
                    row = center_y,
                    col = center_x,
                    width = window_w_int,
                    height = window_h_int,
                }
                end,
        },
        width = function()
          return math.floor(vim.opt.columns:get() * WIDTH_RATIO)
        end,
    },
})

Dumb question: are the borders losing their radius due to border = 'single'? We are defaulting them to rounded...

Hey no need to call my question dumb, let's keep this place civil! :) Sorry btw
for missing this option, and thanks a lot for solving the problem.

@gegoune
Copy link
Collaborator

gegoune commented Dec 3, 2022

Dumb question: are the borders losing their radius due to border = 'single'? We are defaulting them to rounded...

Hey no need to call my question dumb, let's keep this place civil! :) Sorry btw for missing this option, and thanks a lot for solving the problem.

I am pretty sure Alex meant his own question.

@kryzar
Copy link

kryzar commented Dec 3, 2022

I am pretty sure Alex meant his own question.

Oh in that case I sincerely apologize for the misunderstanding. My bad.

@alex-courtis
Copy link
Member Author

Unfortunately, borders lost their radius. How can I add the original radius? Or even better, is there or will be a more automatic, better way to center the floating window?

I'm actually a little confused about how the border became rounded at all as your config should have always applied single.

It's working as expected now 🤷

@kryzar
Copy link

kryzar commented Dec 4, 2022

As border = 'rounded' is the default, isn't it a bug that simply removing the border attribute renders this? Or am I mistaken?

Screenshot from 2022-12-04 12-29-56

@kryzar
Copy link

kryzar commented Dec 6, 2022

(Just a quick message to mention that I did not forget about the recipe; as I am very busy, I will write it at the end of the week.)

@alex-courtis
Copy link
Member Author

As border = 'rounded' is the default, isn't it a bug that simply removing the border attribute renders this? Or am I mistaken?

Of course. It's using vim default as the nvim-tree default table is completely removed.

@MostHated
Copy link

Hello all. My apologies if this is a silly question, I am still quite new to nvim. I have a question in regard to this floating window. If I pass a directory to neovim upon running it, I will get a nvim-tree, but it is not the floating one. If I hit my tree toggle command to hide it and then reopen it, it will show the floating one (demonstrated below). Is there something different I have to do, such as delaying the initial loading of the tree until after all of the settings have 'taken'?

2023-01-09.18-38-48.mp4

@alex-courtis
Copy link
Member Author

Hello all. My apologies if this is a silly question, I am still quite new to nvim. I have a question in regard to this floating window. If I pass a directory to neovim upon running it, I will get a nvim-tree, but it is not the floating one. If I hit my tree toggle command to hide it and then reopen it, it will show the floating one (demonstrated below). Is there something different I have to do, such as delaying the initial loading of the tree until after all of the settings have 'taken'?

That's a bug. Startup is problematic and experiences many race conditions.

A new mechanism is incoming: #1669

@pedro-psb
Copy link

pedro-psb commented Jun 22, 2023

I've found that when using the hjkl recipe, hitting h exits the floating window (H works fine). Any workaround suggestions?

And thanks, this feature is great :)

@gegoune
Copy link
Collaborator

gegoune commented Jun 22, 2023

Please open new issue (or discussion, more likely, as recipes from wiki aren't really "official") rather then commenting on months old pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] Allow floating window dimensions be customizable on demand
6 participants