Skip to content

Commit 97e7f8c

Browse files
committed
shift displayed columns when all columns on screen are hidden
1 parent 3ecf6e0 commit 97e7f8c

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

gui/manipulator.lua

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ end
141141
-- Column
142142
--
143143

144+
local DEFAULT_DATA_WIDTH = 4
145+
local DEFAULT_COL_OVERSCAN = 14
146+
144147
Column = defclass(Column, widgets.Panel)
145148
Column.ATTRS{
146149
label=DEFAULT_NIL,
147150
group='',
148151
label_inset=0,
149-
data_width=4,
152+
data_width=DEFAULT_DATA_WIDTH,
150153
hidden=DEFAULT_NIL,
151154
shared=DEFAULT_NIL,
152155
data_fn=DEFAULT_NIL,
@@ -161,7 +164,7 @@ local CH_UP = string.char(30)
161164
local CH_DN = string.char(31)
162165

163166
function Column:init()
164-
self.frame = utils.assign({t=0, b=0, l=0, w=14}, self.frame or {})
167+
self.frame = utils.assign({t=0, b=0, l=0, w=DEFAULT_COL_OVERSCAN}, self.frame or {})
165168

166169
local function show_menu()
167170
self.subviews.col_menu:show()
@@ -767,7 +770,27 @@ function Spreadsheet:jump_to_group(group)
767770
end
768771

769772
function Spreadsheet:jump_to_col(idx)
773+
idx = math.min(idx, #self.cols.subviews)
774+
idx = math.max(idx, 1)
770775
self.left_col = idx
776+
if self.cols.subviews[idx].hidden then
777+
local found = false
778+
for shifted_idx=self.left_col-1,1,-1 do
779+
if not self.cols.subviews[shifted_idx].hidden then
780+
self.left_col = shifted_idx
781+
found = true
782+
break
783+
end
784+
end
785+
if not found then
786+
for shifted_idx=self.left_col+1,#self.cols.subviews do
787+
if not self.cols.subviews[shifted_idx].hidden then
788+
self.left_col = shifted_idx
789+
break
790+
end
791+
end
792+
end
793+
end
771794
self:updateLayout()
772795
end
773796

@@ -868,7 +891,11 @@ function Spreadsheet:render(dc)
868891
if self.shared.refresh_headers then
869892
self:update_headers()
870893
end
871-
self:updateLayout()
894+
if self.cols.subviews[self.left_col].hidden then
895+
self:jump_to_col(self.left_col)
896+
else
897+
self:updateLayout()
898+
end
872899
end
873900
local page_top = self.namelist.page_top
874901
local selected = self.namelist:getSelected()
@@ -881,23 +908,16 @@ function Spreadsheet:render(dc)
881908
end
882909

883910
function Spreadsheet:get_num_visible_cols()
884-
local count = 0
885-
for _,col in ipairs(self.cols.subviews) do
886-
if col.visible then
887-
count = count + 1
888-
end
889-
end
890-
return count
911+
local rect = self.frame_rect
912+
if not rect then return 1 end
913+
local other_width = self.subviews.name.data_width + (DEFAULT_COL_OVERSCAN - DEFAULT_DATA_WIDTH)
914+
local width = rect.width - other_width
915+
return width // (DEFAULT_DATA_WIDTH + 1)
891916
end
892917

893918
function Spreadsheet:onInput(keys)
894919
if keys.KEYBOARD_CURSOR_LEFT then
895-
for idx=self.left_col-1,1,-1 do
896-
if not self.cols.subviews[idx].hidden then
897-
self:jump_to_col(idx)
898-
break
899-
end
900-
end
920+
self:jump_to_col(self.left_col-1)
901921
elseif keys.KEYBOARD_CURSOR_LEFT_FAST then
902922
local remaining = self:get_num_visible_cols()
903923
local target_col = self.left_col
@@ -1253,14 +1273,14 @@ ManipulatorOverlay.ATTRS{
12531273
default_pos={x=50, y=-6},
12541274
default_enabled=true,
12551275
viewscreens='dwarfmode/Info/CREATURES/CITIZEN',
1256-
frame={w=34, h=1},
1276+
frame={w=35, h=1},
12571277
}
12581278

12591279
function ManipulatorOverlay:init()
12601280
self:addviews{
12611281
widgets.TextButton{
12621282
frame={t=0, l=0},
1263-
label='DFHack citizen interface',
1283+
label='DFHack citizen management',
12641284
key='CUSTOM_CTRL_N',
12651285
on_activate=function() dfhack.run_script('gui/manipulator') end,
12661286
},

0 commit comments

Comments
 (0)