finished application laucnher and bluetooth widget

This commit is contained in:
Rene Kievits
2022-11-27 10:58:27 +01:00
parent c6341f84e7
commit 10f56a7273
77 changed files with 2379 additions and 4938 deletions

View File

@@ -0,0 +1,395 @@
---------------------------------------------------------------------------
-- A widget to write text in
--@DOC_wibox_widget_defaults_inputtextbox_EXAMPLE@
--
-- @author Rene Kievits
-- @copyright 2022 Rene Kievits
-- @widgetmod wibox.widget.inputbox
-- @supermodule wibox.widget.textbox
---------------------------------------------------------------------------
local base = require("wibox.widget.base")
local gdebug = require("gears.debug")
local gfs = require("gears.filesystem")
local gobject = require("gears.object")
local gstring = require("gears.string")
local beautiful = require("beautiful")
local keygrabber = require("awful.keygrabber")
local lgi = require("lgi")
local gtable = require("gears.table")
local wibox = require("wibox")
local abutton = require("awful.button")
local setmetatable = setmetatable
local capi =
{
selection = selection,
mousegrabber = mousegrabber,
mouse = mouse,
}
local inputbox = { mt = {} }
local function text_with_cursor(text, cursor_pos, self)
local char, spacer, text_start, text_end
local cursor_fg = beautiful.inputbox_cursor_fg or "#313131"
local cursor_bg = beautiful.inputbox_cursor_bg or "#0dccfc"
local text_color = beautiful.inputbox_fg or "#ffffff"
local placeholder_text = beautiful.inputbox_placeholder_text or "Type here..."
local placeholder_fg = beautiful.inputbox_placeholder_fg or "#777777"
local highlight_bg = beautiful.inputbox_highlight_bg or "#35ffe4"
local highlight_fg = beautiful.inputbox_highlight_fg or "#000000"
if text == "" then
return "<span foreground='" .. placeholder_fg .. "'>" .. placeholder_text .. "</span>"
end
if #text < cursor_pos then
char = " "
spacer = ""
text_start = gstring.xml_escape(text)
text_end = ""
else
local offset = 0
if #text:sub(cursor_pos, cursor_pos) == -1 then
offset = 1
end
char = gstring.xml_escape(text:sub(cursor_pos, cursor_pos + offset))
spacer = " "
text_start = gstring.xml_escape(text:sub(1, cursor_pos - 1))
text_end = gstring.xml_escape(text:sub(cursor_pos + offset + 1))
end
if self._private.highlight and self._private.highlight.cur_pos_start and self._private.highlight.cur_pos_end then
-- split the text into 3 parts based on the highlight and cursor position
local text_start_highlight = gstring.xml_escape(text:sub(1, self._private.highlight.cur_pos_start - 1))
local text_highlighted = gstring.xml_escape(text:sub(self._private.highlight.cur_pos_start,
self._private.highlight.cur_pos_end))
local text_end_highlight = gstring.xml_escape(text:sub(self._private.highlight.cur_pos_end + 1))
return "<span foreground='" .. text_color .. "'>" .. text_start_highlight .. "</span>" ..
"<span foreground='" .. highlight_fg .. "' background='" .. highlight_bg .. "'>" ..
text_highlighted ..
"</span>" .. "<span foreground='" .. text_color .. "'>" .. text_end_highlight .. "</span>"
else
return "<span foreground='" .. text_color .. "'>" .. text_start .. "</span>" ..
"<span background='" .. cursor_bg .. "' foreground='" .. cursor_fg .. "'>" ..
char .. "</span>" .. "<span foreground='" .. text_color .. "'>" .. text_end .. spacer .. "</span>"
end
end
--- Clears the current text
function inputbox:clear()
self:set_text("")
end
function inputbox:get_text()
return self._private.text or ""
end
function inputbox:set_text(text)
self._private.text = text
self:emit_signal("property::text", text)
end
--- Stop the keygrabber and mousegrabber
function inputbox:stop()
self:emit_signal("stopped")
keygrabber.stop()
capi.mousegrabber.stop()
end
function inputbox:focus()
keygrabber.stop()
if not keygrabber.is_running then
self:run()
end
-- Stops the mousegrabber when not clicked on the widget
--[[ capi.mousegrabber.run(
function(m)
if m.buttons[1] then
if capi.mouse.current_wibox ~= self:get_widget().widget then
self:emit_signal("keygrabber::stop", "")
return false
end
end
return true
end, "left_ptr"
) ]]
self:connect_signal("button::press", function()
if capi.mouse.current_widget ~= self then
self:emit_signal("keygrabber::stop", "")
end
end)
end
function inputbox:run()
if not self._private.text then self._private.text = "" end
-- Init the cursor position, but causes on refocus the cursor to move to the left
local cursor_pos = #self:get_text() + 1
self:emit_signal("started")
-- Init and reset(when refocused) the highlight
self._private.highlight = {}
-- Emitted when the keygrabber is stopped
self:connect_signal("cancel", function()
self:stop()
self:emit_signal("stopped")
end)
-- Emitted when the keygrabber should submit the text
self:connect_signal("submit", function(text)
self:stop()
self:emit_signal("stopped", text)
end)
self:emit_signal("key_pressed", "B", "A")
keygrabber.run(function(mod, key, event)
local mod_keys = {}
for _, v in ipairs(mod) do
mod_keys[v] = true
end
if not (event == "press") then return end
--Escape cases
-- Just quit and leave the text as is
if (not mod_keys.Control) and (key == "Escape") then
self:emit_signal("cancel")
elseif (not mod_keys.Control and key == "KP_Enter") or (not mod_keys.Control and key == "Return") then
self:emit_signal("submit", self:get_text())
self:set_text("")
end
-- All shift, control or key cases
if mod_keys.Shift then
if key == "Left" then
if cursor_pos > 1 then
if not self._private.highlight.cur_pos_start then
self._private.highlight.cur_pos_start = cursor_pos - 1
end
if not self._private.highlight.cur_pos_end then
self._private.highlight.cur_pos_end = cursor_pos
end
if self._private.highlight.cur_pos_start < cursor_pos then
self._private.highlight.cur_pos_end = self._private.highlight.cur_pos_end - 1
else
self._private.highlight.cur_pos_start = self._private.highlight.cur_pos_start - 1
end
cursor_pos = cursor_pos - 1
end
elseif key == "Right" then
if #self._private.text >= cursor_pos then
if not self._private.highlight.cur_pos_end then
self._private.highlight.cur_pos_end = cursor_pos - 1
end
if not self._private.highlight.cur_pos_start then
self._private.highlight.cur_pos_start = cursor_pos
end
if self._private.highlight.cur_pos_end <= cursor_pos then
self._private.highlight.cur_pos_end = self._private.highlight.cur_pos_end + 1
else
self._private.highlight.cur_pos_start = self._private.highlight.cur_pos_start + 1
end
cursor_pos = cursor_pos + 1
if cursor_pos > #self._private.text + 1 then
self._private.highlight = {}
end
end
else
if key:wlen() == 1 then
self:set_text(self._private.text:sub(1, cursor_pos - 1) ..
string.upper(key) .. self._private.text:sub(cursor_pos))
cursor_pos = cursor_pos + 1
end
end
elseif mod_keys.Control then
if key == "a" then
-- Mark the entire text
self._private.highlight = {
cur_pos_start = 1,
cur_pos_end = #self._private.text
}
elseif key == "c" then
-- TODO: Copy the highlighted text when the selection setter gets implemented
elseif key == "v" then
local sel = capi.selection()
if sel then
sel = sel:gsub("\n", "")
if self._private.highlight and self._private.highlight.cur_pos_start and
self._private.highlight.cur_pos_end then
-- insert the text into the selected part
local text_start = self._private.text:sub(1, self._private.highlight.cur_pos_start - 1)
local text_end = self._private.text:sub(self._private.highlight.cur_pos_end + 1)
self:set_text(text_start .. sel .. text_end)
self._private.highlight = {}
cursor_pos = #text_start + #sel + 1
else
self:set_text(self._private.text:sub(1, cursor_pos - 1) ..
sel .. self._private.text:sub(cursor_pos))
cursor_pos = cursor_pos + #sel
end
end
elseif key == "x" then
--TODO: "cut". Copy selected then clear text, this requires to add the c function first.
self._private.highlight = {}
elseif key == "Left" then
-- Find all spaces
local spaces = {}
local t, i = self._private.text, 0
while t:find("%s") do
i = t:find("%s")
table.insert(spaces, i)
t = t:sub(1, i - 1) .. "-" .. t:sub(i + 1)
end
local cp = 1
for _, v in ipairs(spaces) do
if (v < cursor_pos) then
cp = v
end
end
cursor_pos = cp
elseif key == "Right" then
local next_space = self._private.text:sub(cursor_pos):find("%s")
if next_space then
cursor_pos = cursor_pos + next_space
else
cursor_pos = #self._private.text + 1
end
end
else
if key == "BackSpace" then
-- If text is highlighted delete that, else just delete the character to the left
if self._private.highlight and self._private.highlight.cur_pos_start and
self._private.highlight.cur_pos_end then
local text_start = self._private.text:sub(1, self._private.highlight.cur_pos_start - 1)
local text_end = self._private.text:sub(self._private.highlight.cur_pos_end + 1)
self:set_text(text_start .. text_end)
self._private.highlight = {}
cursor_pos = #text_start + 1
else
if cursor_pos > 1 then
self:set_text(self._private.text:sub(1, cursor_pos - 2) ..
self._private.text:sub(cursor_pos))
cursor_pos = cursor_pos - 1
end
end
elseif key == "Delete" then
-- If text is highlighted delete that, else just delete the character to the right
if self._private.highlight and self._private.highlight.cur_pos_start and
self._private.highlight.cur_pos_end then
local text_start = self._private.text:sub(1, self._private.highlight.cur_pos_start - 1)
local text_end = self._private.text:sub(self._private.highlight.cur_pos_end + 1)
self:set_text(text_start .. text_end)
self._private.highlight = {}
cursor_pos = #text_start + 1
else
if cursor_pos <= #self._private.text then
self:set_text(self._private.text:sub(1, cursor_pos - 1) ..
self._private.text:sub(cursor_pos + 1))
end
end
elseif key == "Left" then
-- Move cursor ro the left
if cursor_pos > 1 then
cursor_pos = cursor_pos - 1
end
self._private.highlight = {}
elseif key == "Right" then
-- Move cursor to the right
if cursor_pos <= #self._private.text then
cursor_pos = cursor_pos + 1
end
self._private.highlight = {}
else
-- Print every alphanumeric key
-- It seems like gears.xmlescape doesn't support non alphanumeric characters
if key:wlen() == 1 then
self:set_text(self._private.text:sub(1, cursor_pos - 1) ..
key .. self._private.text:sub(cursor_pos))
cursor_pos = cursor_pos + #key
end
end
-- Make sure the cursor cannot go out of bounds
if cursor_pos < 1 then
cursor_pos = 1
elseif cursor_pos > #self._private.text + 1 then
cursor_pos = #self._private.text + 1
end
end
-- Update cycle
self.text = text_with_cursor(self:get_text(), cursor_pos, self)
-- using self:emit_signal... results in nil tables beeing send
awesome.emit_signal("inputbox::key_pressed", mod_keys, key)
end)
end
function inputbox.new(args)
args = args or {}
local w = wibox.widget.textbox(args.text or "")
gtable.crush(w, inputbox, true)
w:buttons(
gtable.join {
abutton({}, 1, function()
w:focus()
end),
abutton({}, 3, function()
-- TODO: Figure out how to paste with highlighted support
-- Maybe with a signal?
end)
}
)
-- Change the cursor to "xterm" on hover over
local old_cursor, old_wibox
w:connect_signal(
"mouse::enter",
function()
local wid = capi.mouse.current_wibox
if wid then
old_cursor, old_wibox = wid.cursor, wid
wid.cursor = "xterm"
end
end
)
-- Change the cursor back once leaving the widget
w:connect_signal(
"mouse::leave",
function()
old_wibox.cursor = old_cursor
old_wibox = nil
end
)
w.text = text_with_cursor("", 1, w)
return w
end
function inputbox.mt:__call(...)
return inputbox.new(...)
end
return setmetatable(inputbox, inputbox.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@@ -1,282 +0,0 @@
---------------------------------------------------------------------------
-- A widget to write text in
--@DOC_wibox_widget_defaults_inputtextbox_EXAMPLE@
--
-- @author Rene Kievits
-- @copyright 2022, Rene Kievits
-- @widgetmod wibox.widget.inputtextbox
-- @supermodule wibox.widget.base
---------------------------------------------------------------------------
local base = require("wibox.widget.base")
local gdebug = require("gears.debug")
local gfs = require("gears.filesystem")
local gobject = require("gears.object")
local gstring = require("gears.string")
local beautiful = require("beautiful")
local keygrabber = require("awful.keygrabber")
local lgi = require("lgi")
local gtable = require("gears.table")
local wibox = require("wibox")
local setmetatable = setmetatable
local inputtextbox = { mt = {} }
--Private data
local data = {}
data.history = {}
local search_term = nil
local function itera(inc, a, i)
i = i + inc
local v = a[i]
if v then return i, v end
end
--- Load history file in history table
-- @param id The data.history identifier which is the path to the filename.
-- @param[opt] max The maximum number of entried in file.
local function history_check_load(id, max)
if id and id ~= "" and not data.history[id] then
data.history[id] = { max = 50, table = {} }
if max then
data.history[id].max = max
end
local f = io.open(id, "r")
if not f then return end
for line in f:lines() do
if gtable.hasitem(data.histroy[id].table, line) then
if #data.history[id].table >= data.history[id].max then
break
end
end
end
f:close()
end
end
local function is_word_char(c)
if string.find(c, "[{[(,.:;_-+=@/ ]") then
return false
else
return true
end
end
local function cword_start(s, pos)
local i = pos
if i > 1 then
i = i - 1
end
while i >= 1 and not is_word_char(s:sub(i, i)) do
i = i - 1
end
while i >= 1 and is_word_char(s:sub(i, i)) do
i = i - 1
end
if i <= #s then
i = i + 1
end
return i
end
local function cword_end(s, pos)
local i = pos
while i <= #s and not is_word_char(s:sub(i, i)) do
i = i + 1
end
while i <= #s and is_word_char(s:sub(i, i)) do
i = i + 1
end
return i
end
--- Save history table in history file
-- @param id The data.history identifier
local function history_save(id)
if data.history[id] then
gfs.make_parent_directories(id)
local f = io.open(id, "w")
if not f then
gdebug.print_warning("Failed to write the history to " .. id)
return
end
for i = 1, math.min(#data.history[id].table, data.history[id].max) do
f:write(data.history[id].table[i] .. "\n")
end
f:close()
end
end
--- Return the number of items in history table regarding the id
-- @param id The data.history identifier
-- @return the number of items in history table, -1 if history is disabled
local function history_items(id)
if data.history[id] then
return #data.history[id].table
else
return -1
end
end
--- Add an entry to the history file
-- @param id The data.history identifier
-- @param command The command to add
local function history_add(id, command)
if data.history[id] and command ~= "" then
local index = gtable.hasitem(data.history[id].table, command)
if index == nil then
table.insert(data.history[id].table, command)
-- Do not exceed our max_cmd
if #data.history[id].table > data.history[id].max then
table.remove(data.history[id].table, 1)
end
history_save(id)
else
-- Bump this command to the end of history
table.remove(data.history[id].table, index)
table.insert(data.history[id].table, command)
history_save(id)
end
end
end
local function have_multibyte_char_at(text, position)
return text:sub(position, position):wlen() == -1
end
local function text_with_cursor(args)
local char, spacer, text_start, text_end, ret
local text = args.text or ""
local hint = args.hint or ""
local cursor = args.cursor or ""
local indicator = args.indicator or "|"
if args.select_all then
if #text == 0 then char = " " else char = gstring.xml_escape(text) end
spacer = " "
text_start = ""
text_end = ""
elseif #text < args.cursor_pos then
char = " "
spacer = ""
text_start = gstring.xml_escape(text)
text_end = ""
else
local offset = 0
if have_multibyte_char_at(text, args.cursor_pos) then
offset = 1
end
char = gstring.xml_escape(text:sub(args.cursor_pos, args.cursor_pos + offset))
spacer = " "
text_start = gstring.xml_escape(text:sub(1, args.cursor_pos - 1))
text_end = gstring.xml_escape(text:sub(args.cursor_pos + offset + 1))
end
if args.highlighter then
text_start, text_end = args.highlighter(text_start, text_end)
end
if #text == 0 then
ret = hint .. spacer
else
ret = text_start .. indicator .. text_end .. spacer
end
return ret
end
local function update(self)
self.textbox:set_font(self.font)
self.textbox:set_markup(text_with_cursor {
text = self.text,
hint = self.hint,
cursor = self.cursor,
cursor_pos = self.cursor_pos,
select_all = self.select_all,
indicator = self.indicator,
highlighter = self.highlighter,
})
end
function inputtextbox:start()
self.textbox:set_font(self.font)
self.textbox:set_markup(text_with_cursor {
text = self.text,
hint = self.hint,
cursor = self.cursor,
cursor_pos = self.cursor_pos,
select_all = self.select_all,
indicator = self.indicator,
highlighter = self.highlighter,
})
self._private.grabber = keygrabber.run(
function(modifierts, key, event)
local mod = {}
for _, v in ipairs(modifierts) do mod[v] = true end
--Get out cases
if (mod.Control and (key == "c" or key == "g")) or (not mod.Control and key == "Escape") then
self:stop()
return false
elseif (mod.Control and (key == "j" or key == "m")) then
--callback
return
end
end
)
end
function inputtextbox:stop()
keygrabber.stop(self._private.grabber)
history_save(self.history_path)
return false
end
--- Create a new inputtextbox
--
-- @tparam[opt=""] string text The hint text when there is no input
-- @treturn table A new inputtextbox widget
-- @constructorfct wibox.widget.inputtextbox
local function new(args)
args = args or {}
args.callback = args.callback or nil
args.hint = args.hint or ""
args.font = args.font or beautiful.inputtextbox_font or beautiful.font
args.bg = args.bg or beautiful.inputtextbox_bg or beautiful.bg_normal
args.fg_hint = args.fg_hint or beautiful.inputtextbox_fg_hint or beautiful.fg_normal or "#888888"
args.fg = args.fg or beautiful.inputtextbox_fg or beautiful.fg_normal
args.cursor = args.cursor or "fleur"
args.select_all = args.select_all or false
args.highlighter = args.highlighter or nil
local textbox = wibox.widget.textbox()
textbox:set_text(args.hint or "")
--textbox:set_fg(args.fg_hint or beautiful.fg_hint or "#888888")
--textbox:set_bg(args.bg_normal or beautiful.bg_normal or "#212121")
local ret = gobject({})
ret._private = {}
gtable.crush(ret, inputtextbox)
gtable.crush(ret, args)
return ret
end
function inputtextbox.mt.__call(...)
return new(...)
end
return setmetatable(inputtextbox, inputtextbox.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80