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

[gui/notify] initial implementation #977

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/gui/notify.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
gui/notify
==========

.. dfhack-tool::
:summary: Show notifications for important events.
:tags: fort interface

This tool is the configuration interface for the provided overlay. It allows
you to select which notifications to enable for the overlay display. See the
descriptions in the `gui/notify` list for more details on what each
notification is for.

Usage
-----

::

gui/notify

Overlay
-------

This script provides an overlay that shows the currently enabled notifications
(when applicable). If you click on an active notification in the list, it will
zoom the map to the target. If there are multiple targets, each successive
click on the notification will zoom to the next target.
195 changes: 195 additions & 0 deletions gui/notify.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
--@module = true

local gui = require('gui')
local notifications = reqscript('internal/notify/notifications')
local overlay = require('plugins.overlay')
local widgets = require('gui.widgets')

--
-- NotifyOverlay
--

local LIST_MAX_HEIGHT = 5

NotifyOverlay = defclass(NotifyOverlay, overlay.OverlayWidget)
NotifyOverlay.ATTRS{
desc='Shows list of active notifications.',
default_pos={x=1,y=-4},
default_enabled=true,
viewscreens='dwarfmode/Default',
frame={w=30, h=LIST_MAX_HEIGHT+2},
}

function NotifyOverlay:init()
self:addviews{
widgets.Panel{
view_id='panel',
frame_style=gui.MEDIUM_FRAME,
frame_background=gui.CLEAR_PEN,
subviews={
widgets.List{
view_id='list',
frame={t=0, b=0, l=0, r=0},
on_submit=function(_, choice)
choice.state = choice.data.on_click(choice.state)
end,
},
},
},
widgets.ConfigureButton{
frame={t=0, r=2},
on_click=function() dfhack.run_script('gui/notify') end,
}
}
end

function NotifyOverlay:overlay_onupdate()
local choices = {}
local max_width = 20
for _, notification in ipairs(notifications.NOTIFICATIONS_BY_IDX) do
if notifications.config.data[notification.name].enabled then
local str = notification.fn()
if str then
max_width = math.max(max_width, #str)
table.insert(choices, {
text=str,
data=notification,
})
end
end
end
-- +2 for the frame
self.frame.w = max_width + 2
if #choices <= LIST_MAX_HEIGHT then
self.frame.h = #choices + 2
else
self.frame.w = self.frame.w + 3 -- for the scrollbar
self.frame.h = LIST_MAX_HEIGHT + 2
end
local list = self.subviews.list
local idx = 1
local _, selected = list:getSelected()
if selected then
for i, v in ipairs(choices) do
if v.name == selected.name then
idx = i
break
end
end
end
list:setChoices(choices, idx)
self.visible = #choices > 0
end

OVERLAY_WIDGETS = {
panel=NotifyOverlay,
}

--
-- Notify
--

Notify = defclass(Notify, widgets.Window)
Notify.ATTRS{
frame_title='Notification settings',
frame={w=40, h=17},
}

function Notify:init()
self:addviews{
widgets.List{
view_id='list',
frame={t=0, l=0, b=6},
on_submit=self:callback('toggle'),
on_select=function(_, choice)
self.subviews.desc.text_to_wrap = choice.desc
if self.frame_parent_rect then
self:updateLayout()
end
end,
},
widgets.WrappedLabel{
view_id='desc',
frame={b=3, l=0, h=3},
auto_height=false,
},
widgets.HotkeyLabel{
frame={b=0, l=0},
label='Toggle',
key='SELECT',
auto_width=true,
on_activate=function() self:toggle(self.subviews.list:getSelected()) end,
},
widgets.HotkeyLabel{
frame={b=0, l=15},
label='Toggle all',
key='CUSTOM_CTRL_A',
auto_width=true,
on_activate=self:callback('toggle_all'),
},
}

self:refresh()
end

function Notify:refresh()
local choices = {}
for name, conf in pairs(notifications.config.data) do
table.insert(choices, {
name=name,
desc=notifications.NOTIFICATIONS_BY_NAME[name].desc,
enabled=conf.enabled,
text={
('%20s: '):format(name),
{
text=conf.enabled and 'Enabled' or 'Disabled',
pen=conf.enabled and COLOR_GREEN or COLOR_RED,
}
}
})
end
table.sort(choices, function(a, b) return a.name < b.name end)
local list = self.subviews.list
local selected = list:getSelected()
list:setChoices(choices)
list:setSelected(selected)
end

function Notify:toggle(_, choice)
if not choice then return end
notifications.config.data[choice.name].enabled = not choice.enabled
self:refresh()
end

function Notify:toggle_all()
local choice = self.subviews.list:getChoices()[1]
if not choice then return end
local target_state = not choice.enabled
for name in pairs(notifications.NOTIFICATIONS_BY_NAME) do
notifications.config.data[name].enabled = target_state
end
self:refresh()
end

--
-- NotifyScreen
--

NotifyScreen = defclass(NotifyScreen, gui.ZScreen)
NotifyScreen.ATTRS {
focus_path='notify',
}

function NotifyScreen:init()
self:addviews{Notify{}}
end

function NotifyScreen:onDismiss()
view = nil
end

if dfhack_flags.module then
return
end

view = view and view:raise() or NotifyScreen{}:show()
1 change: 1 addition & 0 deletions gui/petitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Petitions.ATTRS {
frame_title='Petitions',
frame={w=110, h=30},
resizable=true,
resize_min={w=70, h=20},
}

function Petitions:init()
Expand Down
Loading
Loading