Skip to content

Commit

Permalink
Add shortcuts to Journal previous/next feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Aug 2, 2024
1 parent 3cfeb81 commit 184ceb6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
52 changes: 13 additions & 39 deletions gui/journal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function JournalWindow:init()
view_id='table_of_contents_panel',
frame={l=0, w=toc_width, t=0, b=1},
visible=toc_visible,
frame_inset={l=0, t=1, b=1, r=1},
frame_inset={l=1, t=0, b=1, r=1},

resize_min={w=20},
resizable=true,
Expand All @@ -61,7 +61,8 @@ function JournalWindow:init()

if not colllapsed then
self.subviews.table_of_contents_panel:reload(
self.subviews.journal_editor:getText()
self.subviews.journal_editor:getText(),
self.subviews.journal_editor:getCursor()
)
end

Expand Down Expand Up @@ -107,39 +108,10 @@ function JournalWindow:init()
}
})

self.subviews.table_of_contents_panel:reload(self.init_text)
end

function JournalWindow:onInput(keys)
if (keys.A_MOVE_N_DOWN) then
local curr_cursor = self.subviews.journal_editor:getCursor()
local section_index, section = self.subviews.table_of_contents_panel:cursorSection(
curr_cursor
)

if section.line_cursor == curr_cursor then
self.subviews.table_of_contents_panel:setSelectedSection(
section_index - 1
)
self.subviews.table_of_contents_panel:submit()
else
self:onTableOfContentsSubmit(section_index, section)
end

return false
elseif (keys.A_MOVE_S_DOWN) then
local section_index = self.subviews.table_of_contents_panel:cursorSection(
self.subviews.journal_editor:getCursor()
)
self.subviews.table_of_contents_panel:setSelectedSection(
section_index + 1
)
self.subviews.table_of_contents_panel:submit()

return false
end

return JournalWindow.super.onInput(self, keys)
self.subviews.table_of_contents_panel:reload(
self.init_text,
self.subviews.journal_editor:getCursor() or self.init_cursor
)
end

function JournalWindow:sanitizeFrame(frame)
Expand Down Expand Up @@ -239,9 +211,8 @@ function JournalWindow:postUpdateLayout()
end

function JournalWindow:onCursorChange(cursor)
local section_index = self.subviews.table_of_contents_panel:cursorSection(
cursor
)
self.subviews.table_of_contents_panel:setCursor(cursor)
local section_index = self.subviews.table_of_contents_panel:currentSection()
self.subviews.table_of_contents_panel:setSelectedSection(section_index)

if self.on_cursor_change ~= nil then
Expand All @@ -250,7 +221,10 @@ function JournalWindow:onCursorChange(cursor)
end

function JournalWindow:onTextChange(text)
self.subviews.table_of_contents_panel:reload(text)
self.subviews.table_of_contents_panel:reload(
text,
self.subviews.journal_editor:getCursor()
)

if self.on_text_change ~= nil then
self.on_text_change(text)
Expand Down
63 changes: 50 additions & 13 deletions internal/journal/table_of_contents.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,81 @@ TableOfContents = defclass(TableOfContents, widgets.Panel)
TableOfContents.ATTRS {
frame_style=INVISIBLE_FRAME,
frame_background = gui.CLEAR_PEN,
on_submit=DEFAULT_NIL
on_submit=DEFAULT_NIL,
text_cursor=DEFAULT_NIL
}

function TableOfContents:init()
self:addviews({
widgets.List{
frame={l=1, t=0, r=1, b=0},
frame={l=0, t=0, r=0, b=3},
view_id='table_of_contents',
choices={},
on_submit=self.on_submit
},
widgets.HotkeyLabel{
frame={b=0},
key='A_MOVE_N_DOWN',
label='Previous Section',
on_activate=self:callback('previousSection'),
},
widgets.HotkeyLabel{
frame={b=1},
key='A_MOVE_S_DOWN',
label='Next Section',
on_activate=self:callback('nextSection'),
}
})
end

function TableOfContents:cursorSection(cursor)
local section_ind = nil
function TableOfContents:previousSection()
local section_cursor, section = self:currentSection()

for ind, choice in ipairs(self.subviews.table_of_contents.choices) do
if choice.line_cursor > cursor then
break
end
section_ind = ind
if section.line_cursor == self.text_cursor then
self.subviews.table_of_contents:setSelected(section_cursor - 1)
end

return section_ind, self.subviews.table_of_contents.choices[section_ind]
self.subviews.table_of_contents:submit()
end

function TableOfContents:nextSection()
local curr_sel = self.subviews.table_of_contents:getSelected()

local target_sel = self.text_cursor and
self:currentSection() + 1 or curr_sel + 1

if curr_sel ~= target_sel then
self.subviews.table_of_contents:setSelected(target_sel)
self.subviews.table_of_contents:submit()
end
end

function TableOfContents:setSelectedSection(section_index)
local curr_sel = self.subviews.table_of_contents:getSelected()

if curr_sel ~= section_index then
self.subviews.table_of_contents:setSelected(section_index)
end
end

function TableOfContents:submit()
return self.subviews.table_of_contents:submit()
function TableOfContents:currentSection()
local section_ind = nil

for ind, choice in ipairs(self.subviews.table_of_contents.choices) do
if choice.line_cursor > self.text_cursor then
break
end
section_ind = ind
end

return section_ind, self.subviews.table_of_contents.choices[section_ind]
end

function TableOfContents:setCursor(cursor)
self.text_cursor = cursor
end

function TableOfContents:reload(text)
function TableOfContents:reload(text, cursor)
if not self.visible then
return
end
Expand All @@ -70,5 +106,6 @@ function TableOfContents:reload(text)
line_cursor = line_cursor + #line + 1
end

self.text_cursor = cursor
self.subviews.table_of_contents:setChoices(sections)
end

0 comments on commit 184ceb6

Please sign in to comment.