Adjust some code + fix initial value not working

This commit is contained in:
Ksaper 2023-03-10 05:38:32 +02:00
parent 881895e200
commit a99eb3ffe0
1 changed files with 22 additions and 17 deletions

View File

@ -31,7 +31,7 @@ local properties = {
"unfocus_keys", "unfocus_on_clicked_outside", "unfocus_on_mouse_leave", "unfocus_on_tag_change",
"focus_on_subject_mouse_enter", "unfocus_on_subject_mouse_leave",
"reset_on_unfocus",
"placeholder", "text", "pattern", "obscure",
"placeholder", "initial", "pattern", "obscure",
"cursor_blink", "cursor_blink_rate","cursor_size", "cursor_bg",
"selection_bg"
}
@ -195,20 +195,21 @@ end
function text_input:set_widget_template(widget_template)
local wp = self._private
self._private.text_widget = widget_template:get_children_by_id("text_role")[1]
self._private.text_widget.forced_width = math.huge
local text_draw = self._private.text_widget.draw
self._private.text_widget:set_text(self:get_text())
wp.text_widget = widget_template:get_children_by_id("text_role")[1]
wp.text_widget.forced_width = math.huge
local text_draw = wp.text_widget.draw
if self:get_initial() then
self:set_text(self:get_initial())
end
local placeholder_widget = widget_template:get_children_by_id("placeholder_role")
if placeholder_widget then
placeholder_widget = placeholder_widget[1]
end
function self._private.text_widget:draw(context, cr, width, height)
function wp.text_widget:draw(context, cr, width, height)
-- Selection bg
local ink_rect, logical_rect = self._private.layout:get_pixel_extents()
local _, logical_rect = self._private.layout:get_pixel_extents()
cr:set_source(gcolor.change_opacity(wp.selection_bg, wp.selection_opacity))
cr:rectangle(
wp.selection_start_x,
@ -219,7 +220,7 @@ function text_input:set_widget_template(widget_template)
cr:fill()
-- Cursor
local ink_rect, logical_rect = self._private.layout:get_pixel_extents()
local _, logical_rect = self._private.layout:get_pixel_extents()
cr:set_source(gcolor.change_opacity(wp.cursor_bg, wp.cursor_opacity))
cr:set_line_width(wp.cursor_width)
cr:move_to(wp.cursor_x, logical_rect.y - 3)
@ -236,19 +237,17 @@ function text_input:set_widget_template(widget_template)
end
end
wp.selecting_text = false
local function on_drag(drawable, lx, ly)
local function on_drag(_, lx, ly)
if not wp.selecting_text and (lx ~= wp.press_pos.lx or ly ~= wp.press_pos.ly) then
self:set_selection_start_index_from_x_y(wp.press_pos.lx, wp.press_pos.ly)
self:set_selection_end_index(self._private.selection_start)
self:set_selection_end_index(wp.selection_start)
wp.selecting_text = true
elseif wp.selecting_text then
self:set_selection_end_index_from_x_y(lx - wp.offset.x, ly - wp.offset.y)
end
end
self._private.text_widget:connect_signal("button::press", function(_, lx, ly, button, mods, find_widgets_result)
wp.text_widget:connect_signal("button::press", function(_, lx, ly, button, mods, find_widgets_result)
if button == 1 then
wp.press_pos = { lx = lx, ly = ly }
wp.offset = { x = find_widgets_result.x, y = find_widgets_result.y }
@ -256,7 +255,7 @@ function text_input:set_widget_template(widget_template)
end
end)
self._private.text_widget:connect_signal("button::release", function(_, lx, ly, button, mods, find_widgets_result)
wp.text_widget:connect_signal("button::release", function(_, lx, ly, button, mods, find_widgets_result)
find_widgets_result.drawable:disconnect_signal("mouse::move", on_drag)
if not wp.selecting_text then
self:set_cursor_index_from_x_y(lx, ly)
@ -266,7 +265,7 @@ function text_input:set_widget_template(widget_template)
self:focus()
end)
self._private.text_widget:connect_signal("mouse::enter", function()
wp.text_widget:connect_signal("mouse::enter", function()
capi.root.cursor("xterm")
local wibox = capi.mouse.current_wibox
if wibox then
@ -274,7 +273,7 @@ function text_input:set_widget_template(widget_template)
end
end)
self._private.text_widget:connect_signal("mouse::leave", function(_, find_widgets_result)
wp.text_widget:connect_signal("mouse::leave", function(_, find_widgets_result)
if self:get_focused() == false then
capi.root.cursor("left_ptr")
local wibox = capi.mouse.current_wibox
@ -314,6 +313,11 @@ function text_input:toggle_obscure()
self:set_obscure(not self._private.obscure)
end
function text_input:set_initial(initial)
self._private.initial = initial
self:set_text(initial)
end
function text_input:update_text(text)
if self:get_mode() == "insert" then
self:insert_text(text)
@ -738,6 +742,7 @@ local function new()
wp.selection_start_y = 0
wp.selection_end_y = 0
wp.selection_opacity = 0
wp.selecting_text = false
wp.unfocus_keys = { "Escape", "Return" }
wp.unfocus_on_clicked_outside = true