awesome icon indicating copy to clipboard operation
awesome copied to clipboard

How can I automatically resize wibar?

Open none9632 opened this issue 3 years ago • 2 comments

I have configured wibar so that the output is automatically updated. But there is a problem with the fact that the output can go beyond the borders of wibar. How can this problem be solved?

Example:

region-20220324-215826

region-20220324-215911

Code:

mycpu = wibox.widget {
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

mycputemp = wibox.widget {
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

myram = wibox.widget {
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

mybattery = wibox.widget {
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   align  = "center",
   valign = "center",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.wibar {
         screen  = s,
         align   = "right",
         margins = {
            top   = 14,
            right = 14
         },
         width   = 586,
         height  = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",

               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           cpu_out = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           ram_out = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           bat_out = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)

      mycpu.markup     = cpu_out
      myram.markup     = ram_out
      mybattery.markup = bat_out
   end
}

none9632 avatar Mar 24 '22 22:03 none9632

Wibar doesn't currently handle automatic resize based on its content length. You'll have to keep track of the size of the content manually and size the wibar manually when the contents change.

I know @Elv13 had plan to make the wibar derivate from popup (which have auto-resize capabilities), but I'm not aware of progress on it.

Aire-One avatar Mar 28 '22 17:03 Aire-One

The best solution I could find was to use awful.popup instead of awful.wibar.

mycpu = wibox.widget { widget = wibox.widget.textbox }

mycputemp = wibox.widget { widget = wibox.widget.textbox }

myram = wibox.widget { widget = wibox.widget.textbox }

mybattery = wibox.widget { widget = wibox.widget.textbox }

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.popup {
         screen  = s,
         placement = function(c)
            return awful.placement.top_right(c, { margins = 14 })
         end,
         minimum_height = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",
               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }

      s.mywibox:struts({top = s.mywibox.minimum_height + beautiful.useless_gap * 2})
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           mycpu.markup = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           myram.markup = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           mybattery.markup = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)
   end
}

none9632 avatar Mar 29 '22 05:03 none9632