implement minimize button

develop
Myk Taylor 2023-04-23 19:44:22 -07:00
parent 2884abf2a7
commit a481520efd
No known key found for this signature in database
1 changed files with 60 additions and 57 deletions

@ -244,46 +244,75 @@ local function do_export()
end end
-------------------- --------------------
-- StockpilesOverlay -- MinimizeButton
-------------------- --------------------
MinimizeButton = defclass(MinimizeButton, widgets.Panel) MinimizeButton = defclass(MinimizeButton, widgets.Widget)
MinimizeButton.ATTRS{label_pos='left', get_minimized_fn=DEFAULT_NIL, on_click=DEFAULT_NIL} MinimizeButton.ATTRS{
label_unminimized='minimize',
label_minimized='restore',
label_pos='left',
get_minimized_fn=DEFAULT_NIL,
on_click=DEFAULT_NIL,
}
function MinimizeButton:init() function MinimizeButton:init()
local show_label, hide_label = 'show', 'hide' self.hovered = false
local label_width = math.max(#show_label, #hide_label)
local is_hovered = function() return self.hovered end
local is_not_hovered = function() return not self.hovered end
local get_action_symbol = function()
return self.get_minimized_fn() and string.char(31) or string.char(30)
end
local get_label = function()
local label = self.get_minimized_fn() and self.label_minimized or self.label_unminimized
return (' %s '):format(label)
end
local hovered_text = {'[', {text=get_action_symbol}, ']'}
table.insert(hovered_text,
self.label_pos == 'left' and 1 or nil,
{text=get_label, hpen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_WHITE}})
self:addviews{ self:addviews{
widgets.Label{ widgets.Label{
frame={t=0, l=0, w=1, h=1}, view_id='unhovered_label',
text='[' .. string.char(30) .. ']', frame={t=0, r=0, w=3, h=1},
text={'[', {text=get_action_symbol}, ']'},
text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_LIGHTRED}, text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_LIGHTRED},
text_hpen=dfhack.pen.parse{fg=COLOR_WHITE, bg=COLOR_RED}, text_hpen=dfhack.pen.parse{fg=COLOR_WHITE, bg=COLOR_RED},
visible=self.get_minimized_fn, on_click=function() self.on_click() self:updateLayout() end,
visible=is_not_hovered,
}, widgets.Label{ }, widgets.Label{
frame={t=0, l=1, w=1, h=1}, view_id='hovered_label',
text={{width=label_width}, frame={t=0, r=0, h=1},
'[', { text=hovered_text,
text=function() auto_width=true,
return self.get_minimized_fn() and string.char(31) or string.char(30)
end,
}, ']',
},
text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_LIGHTRED}, text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_LIGHTRED},
text_hpen=dfhack.pen.parse{fg=COLOR_WHITE, bg=COLOR_RED}, text_hpen=dfhack.pen.parse{fg=COLOR_WHITE, bg=COLOR_RED},
on_click=self.on_click, on_click=function() self.on_click() self:updateLayout() end,
}, widgets.Label{ visible=is_hovered,
frame={t=0, r=0, w=1, h=1},
text=']',
text_pen=COLOR_RED,
visible=function()
return self.minimized
end,
}, },
} }
end end
function MinimizeButton:onRenderFrame()
local prev_hovered = self.hovered
if self.hovered then
self.hovered = self.subviews.hovered_label:getMousePos()
else
self.hovered = self.subviews.unhovered_label:getMousePos()
end
if self.hovered ~= prev_hovered then
self:updateLayout()
df.global.gps.force_full_display_count = 1
end
end
--------------------
-- StockpilesOverlay
--------------------
StockpilesOverlay = defclass(StockpilesOverlay, overlay.OverlayWidget) StockpilesOverlay = defclass(StockpilesOverlay, overlay.OverlayWidget)
StockpilesOverlay.ATTRS{ StockpilesOverlay.ATTRS{
default_pos={x=53, y=-6}, default_pos={x=53, y=-6},
@ -308,7 +337,7 @@ function StockpilesOverlay:init()
subviews={ subviews={
widgets.HotkeyLabel{ widgets.HotkeyLabel{
frame={t=0, l=0}, frame={t=0, l=0},
label='apply settings', label='import settings',
key='CUSTOM_CTRL_I', key='CUSTOM_CTRL_I',
on_activate=do_import, on_activate=do_import,
}, widgets.HotkeyLabel{ }, widgets.HotkeyLabel{
@ -361,40 +390,14 @@ function StockpilesOverlay:init()
}, },
} }
local minimized_panel = widgets.Panel{ self:addviews{
frame={t=0, r=0, w=3, h=1}, main_panel,
subviews={ MinimizeButton{
widgets.Label{ frame={t=0, r=1},
frame={t=0, l=0, w=1, h=1}, get_minimized_fn=function() return self.minimized end,
text='[', on_click=self:callback('toggleMinimized'),
text_pen=COLOR_RED,
visible=function()
return self.minimized
end,
}, widgets.Label{
frame={t=0, l=1, w=1, h=1},
text={
{
text=function()
return self.minimized and string.char(31) or string.char(30)
end,
},
},
text_pen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_GREY},
text_hpen=dfhack.pen.parse{fg=COLOR_BLACK, bg=COLOR_WHITE},
on_click=self:callback('toggleMinimized'),
}, widgets.Label{
frame={t=0, r=0, w=1, h=1},
text=']',
text_pen=COLOR_RED,
visible=function()
return self.minimized
end,
},
}, },
} }
self:addviews{main_panel, minimized_panel}
end end
function StockpilesOverlay:overlay_onupdate() function StockpilesOverlay:overlay_onupdate()