Add more helper functions + remove useless comments

This commit is contained in:
Ksaper 2023-02-24 23:03:56 +02:00
parent c449681cbe
commit a4df7debbc
1 changed files with 60 additions and 58 deletions

View File

@ -198,12 +198,12 @@ end
local function search(self) local function search(self)
local text = self._private.text local text = self._private.text
local old_pos = self._private.grid:get_widget_position(self._private.active_widget) local old_pos = self:get_grid():get_widget_position(self._private.active_widget)
-- Reset all the matched apps -- Reset all the matched apps
self._private.matched_apps = {} self._private.matched_apps = {}
-- Remove all the grid widgets -- Remove all the grid widgets
self._private.grid:reset() self:get_grid():reset()
if text == "" then if text == "" then
self._private.matched_apps = self._private.all_apps self._private.matched_apps = self._private.all_apps
@ -226,8 +226,8 @@ local function search(self)
end end
for _, app in ipairs(self._private.matched_apps) do for _, app in ipairs(self._private.matched_apps) do
-- Only add the widgets for apps that are part of the first page -- Only add the widgets for apps that are part of the first page
if #self._private.grid.children + 1 <= self._private.max_apps_per_page then if #self:get_grid().children + 1 <= self._private.max_apps_per_page then
self._private.grid:add(app_widget(self, app)) self:get_grid():add(app_widget(self, app))
end end
end end
@ -244,16 +244,16 @@ local function search(self)
-- it will reselect the app whose index is the same as the app index that was previously selected -- it will reselect the app whose index is the same as the app index that was previously selected
-- and if matched_apps.length < current_index it will instead select the app with the greatest index -- and if matched_apps.length < current_index it will instead select the app with the greatest index
if self.try_to_keep_index_after_searching then if self.try_to_keep_index_after_searching then
if self._private.grid:get_widgets_at(old_pos.row, old_pos.col) == nil then if self:get_grid():get_widgets_at(old_pos.row, old_pos.col) == nil then
local app = self._private.grid.children[#self._private.grid.children] local app = self:get_grid().children[#self:get_grid().children]
app:select() app:select()
else else
local app = self._private.grid:get_widgets_at(old_pos.row, old_pos.col)[1] local app = self:get_grid():get_widgets_at(old_pos.row, old_pos.col)[1]
app:select() app:select()
end end
-- Otherwise select the first app on the list -- Otherwise select the first app on the list
elseif #self._private.grid.children > 0 then elseif #self:get_grid().children > 0 then
local app = self._private.grid:get_widgets_at(1, 1)[1] local app = self:get_grid():get_widgets_at(1, 1)[1]
app:select() app:select()
end end
end end
@ -271,36 +271,36 @@ local function page_forward(self, dir)
min_app_index_to_include = 0 min_app_index_to_include = 0
max_app_index_to_include = self._private.apps_per_page max_app_index_to_include = self._private.apps_per_page
elseif self.wrap_app_scrolling then elseif self.wrap_app_scrolling then
local app = self._private.grid:get_widgets_at(1, 1)[1] local app = self:get_grid():get_widgets_at(1, 1)[1]
app:select() app:select()
return return
else else
return return
end end
local pos = self._private.grid:get_widget_position(self._private.active_widget) local pos = self:get_grid():get_widget_position(self._private.active_widget)
-- Remove the current page apps from the grid -- Remove the current page apps from the grid
self._private.grid:reset() self:get_grid():reset()
for index, app in ipairs(self._private.matched_apps) do for index, app in ipairs(self._private.matched_apps) do
-- Only add widgets that are between this range (part of the current page) -- Only add widgets that are between this range (part of the current page)
if index > min_app_index_to_include and index <= max_app_index_to_include then if index > min_app_index_to_include and index <= max_app_index_to_include then
self._private.grid:add(app_widget(self, app)) self:get_grid():add(app_widget(self, app))
end end
end end
if self._private.current_page > 1 or self.wrap_page_scrolling then if self._private.current_page > 1 or self.wrap_page_scrolling then
local app = nil local app = nil
if dir == "down" then if dir == "down" then
app = self._private.grid:get_widgets_at(1, 1)[1] app = self:get_grid():get_widgets_at(1, 1)[1]
elseif dir == "right" then elseif dir == "right" then
app = self._private.grid:get_widgets_at(pos.row, 1) app = self:get_grid():get_widgets_at(pos.row, 1)
if app then if app then
app = app[1] app = app[1]
end end
if app == nil then if app == nil then
app = self._private.grid.children[#self._private.grid.children] app = self:get_grid().children[#self:get_grid().children]
end end
end end
app:select() app:select()
@ -313,17 +313,17 @@ local function page_backward(self, dir)
elseif self.wrap_page_scrolling and #self._private.matched_apps >= self._private.max_apps_per_page then elseif self.wrap_page_scrolling and #self._private.matched_apps >= self._private.max_apps_per_page then
self._private.current_page = self._private.pages_count self._private.current_page = self._private.pages_count
elseif self.wrap_app_scrolling then elseif self.wrap_app_scrolling then
local app = self._private.grid.children[#self._private.grid.children] local app = self:get_grid().children[#self:get_grid().children]
app:select() app:select()
return return
else else
return return
end end
local pos = self._private.grid:get_widget_position(self._private.active_widget) local pos = self:get_grid():get_widget_position(self._private.active_widget)
-- Remove the current page apps from the grid -- Remove the current page apps from the grid
self._private.grid:reset() self:get_grid():reset()
local max_app_index_to_include = self._private.apps_per_page * self._private.current_page local max_app_index_to_include = self._private.apps_per_page * self._private.current_page
local min_app_index_to_include = max_app_index_to_include - self._private.apps_per_page local min_app_index_to_include = max_app_index_to_include - self._private.apps_per_page
@ -331,56 +331,56 @@ local function page_backward(self, dir)
for index, app in ipairs(self._private.matched_apps) do for index, app in ipairs(self._private.matched_apps) do
-- Only add widgets that are between this range (part of the current page) -- Only add widgets that are between this range (part of the current page)
if index > min_app_index_to_include and index <= max_app_index_to_include then if index > min_app_index_to_include and index <= max_app_index_to_include then
self._private.grid:add(app_widget(self, app)) self:get_grid():add(app_widget(self, app))
end end
end end
local app = nil local app = nil
if self._private.current_page < self._private.pages_count then if self._private.current_page < self._private.pages_count then
if dir == "up" then if dir == "up" then
app = self._private.grid.children[#self._private.grid.children] app = self:get_grid().children[#self:get_grid().children]
else else
-- Keep the same row from last page -- Keep the same row from last page
local _, columns = self._private.grid:get_dimension() local _, columns = self:get_grid():get_dimension()
app = self._private.grid:get_widgets_at(pos.row, columns)[1] app = self:get_grid():get_widgets_at(pos.row, columns)[1]
end end
elseif self.wrap_page_scrolling then elseif self.wrap_page_scrolling then
app = self._private.grid.children[#self._private.grid.children] app = self:get_grid().children[#self:get_grid().children]
end end
app:select() app:select()
end end
local function scroll(self, dir) local function scroll(self, dir)
if #self._private.grid.children < 1 then if #self:get_grid().children < 1 then
self._private.active_widget = nil self._private.active_widget = nil
return return
end end
local pos = self._private.grid:get_widget_position(self._private.active_widget) local pos = self:get_grid():get_widget_position(self._private.active_widget)
local can_scroll = false local can_scroll = false
local step_size = 0 local step_size = 0
local if_cant_scroll_func = nil local if_cant_scroll_func = nil
if dir == "up" then if dir == "up" then
can_scroll = self._private.grid:index(self._private.active_widget) > 1 can_scroll = self:get_grid():index(self._private.active_widget) > 1
step_size = -1 step_size = -1
if_cant_scroll_func = function() page_backward(self, "up") end if_cant_scroll_func = function() page_backward(self, "up") end
elseif dir == "down" then elseif dir == "down" then
can_scroll = self._private.grid:index(self._private.active_widget) < #self._private.grid.children can_scroll = self:get_grid():index(self._private.active_widget) < #self:get_grid().children
step_size = 1 step_size = 1
if_cant_scroll_func = function() page_forward(self, "down") end if_cant_scroll_func = function() page_forward(self, "down") end
elseif dir == "left" then elseif dir == "left" then
can_scroll = self._private.grid:get_widgets_at(pos.row, pos.col - 1) ~= nil can_scroll = self:get_grid():get_widgets_at(pos.row, pos.col - 1) ~= nil
step_size = -self._private.grid.forced_num_rows step_size = -self:get_grid().forced_num_rows
if_cant_scroll_func = function() page_backward(self, "left") end if_cant_scroll_func = function() page_backward(self, "left") end
elseif dir == "right" then elseif dir == "right" then
can_scroll = self._private.grid:get_widgets_at(pos.row, pos.col + 1) ~= nil can_scroll = self:get_grid():get_widgets_at(pos.row, pos.col + 1) ~= nil
step_size = self._private.grid.forced_num_cols step_size = self:get_grid().forced_num_cols
if_cant_scroll_func = function() page_forward(self, "right") end if_cant_scroll_func = function() page_forward(self, "right") end
end end
if can_scroll then if can_scroll then
local app = gtable.cycle_value(self._private.grid.children, self._private.active_widget, step_size) local app = gtable.cycle_value(self:get_grid().children, self._private.active_widget, step_size)
app:select() app:select()
else else
if_cant_scroll_func() if_cant_scroll_func()
@ -533,7 +533,7 @@ local function build_widget(self)
widget = widget widget = widget
} }
self._private.grid:connect_signal("button::press", function(_, lx, ly, button, mods, find_widgets_result) self:get_grid():connect_signal("button::press", function(_, lx, ly, button, mods, find_widgets_result)
if button == 4 then if button == 4 then
self:scroll_up() self:scroll_up()
elseif button == 5 then elseif button == 5 then
@ -541,7 +541,7 @@ local function build_widget(self)
end end
end) end)
self._private.prompt:connect_signal("text::changed", function(_, text) self:get_prompt():connect_signal("text::changed", function(_, text)
if text == self._private.text then if text == self._private.text then
return return
end end
@ -550,7 +550,7 @@ local function build_widget(self)
self._private.search_timer:again() self._private.search_timer:again()
end) end)
self._private.prompt:connect_signal("key::release", function(_, mod, key, cmd) self:get_prompt():connect_signal("key::release", function(_, mod, key, cmd)
if key == "Escape" then if key == "Escape" then
self:hide() self:hide()
end end
@ -573,11 +573,10 @@ local function build_widget(self)
end end
end) end)
self._private.max_apps_per_page = self._private.grid.forced_num_cols * self._private.grid.forced_num_rows self._private.max_apps_per_page = self:get_grid().forced_num_cols * self:get_grid().forced_num_rows
self._private.apps_per_page = self._private.max_apps_per_page self._private.apps_per_page = self._private.max_apps_per_page
end end
-- Sets favorites
function app_launcher:set_favorites(favorites) function app_launcher:set_favorites(favorites)
self.favorites = favorites self.favorites = favorites
sort_apps(self) sort_apps(self)
@ -585,40 +584,34 @@ function app_launcher:set_favorites(favorites)
search(self) search(self)
end end
--- Scrolls up
function app_launcher:scroll_up() function app_launcher:scroll_up()
scroll(self, "up") scroll(self, "up")
end end
--- Scrolls down
function app_launcher:scroll_down() function app_launcher:scroll_down()
scroll(self, "down") scroll(self, "down")
end end
--- Scrolls to the left
function app_launcher:scroll_left() function app_launcher:scroll_left()
scroll(self, "left") scroll(self, "left")
end end
--- Scrolls to the right
function app_launcher:scroll_right() function app_launcher:scroll_right()
scroll(self, "right") scroll(self, "right")
end end
--- Shows the app launcher
function app_launcher:show() function app_launcher:show()
if self.show_on_focused_screen then if self.show_on_focused_screen then
self._private.widget.screen = awful.screen.focused() self:get_widget().screen = awful.screen.focused()
end end
self._private.widget.visible = true self:get_widget().visible = true
self._private.prompt:start() self:get_prompt():start()
self:emit_signal("visibility", true) self:emit_signal("visibility", true)
end end
--- Hides the app launcher
function app_launcher:hide() function app_launcher:hide()
if self._private.widget.visible == false then if self:get_widget().visible == false then
return return
end end
@ -626,23 +619,21 @@ function app_launcher:hide()
self:reset() self:reset()
end end
self._private.widget.visible = false self:get_widget().visible = false
self._private.prompt:stop() self:get_prompt():stop()
self:emit_signal("visibility", false) self:emit_signal("visibility", false)
end end
--- Toggles the app launcher
function app_launcher:toggle() function app_launcher:toggle()
if self._private.widget.visible then if self:get_widget().visible then
self:hide() self:hide()
else else
self:show() self:show()
end end
end end
-- Reset the app page into the initial state
function app_launcher:reset() function app_launcher:reset()
self._private.grid:reset() self:get_grid():reset()
self._private.matched_apps = self._private.all_apps self._private.matched_apps = self._private.all_apps
self._private.apps_per_page = self._private.max_apps_per_page self._private.apps_per_page = self._private.max_apps_per_page
self._private.pages_count = math.ceil(#self._private.all_apps / self._private.apps_per_page) self._private.pages_count = math.ceil(#self._private.all_apps / self._private.apps_per_page)
@ -651,19 +642,30 @@ function app_launcher:reset()
for index, app in ipairs(self._private.all_apps) do for index, app in ipairs(self._private.all_apps) do
-- Only add the apps that are part of the first page -- Only add the apps that are part of the first page
if index <= self._private.apps_per_page then if index <= self._private.apps_per_page then
self._private.grid:add(app_widget(self, app)) self:get_grid():add(app_widget(self, app))
else else
break break
end end
end end
local app = self._private.grid:get_widgets_at(1, 1)[1] local app = self:get_grid():get_widgets_at(1, 1)[1]
app:select() app:select()
self._private.prompt:set_text("") self:get_prompt():set_text("")
end
function app_launcher:get_widget()
return self._private.widget
end
function app_launcher:get_prompt()
return self._private.prompt
end
function app_launcher:get_grid()
return self._private.grid
end end
-- Returns a new app launcher
local function new(args) local function new(args)
args = args or {} args = args or {}