fix highlight
This commit is contained in:
@@ -1,23 +1,13 @@
|
|||||||
local Pango = require('lgi').Pango
|
local Pango = require('lgi').Pango
|
||||||
local PangoCairo = require('lgi').PangoCairo
|
local PangoCairo = require('lgi').PangoCairo
|
||||||
local abutton = require('awful.button')
|
|
||||||
local akey = require('awful.key')
|
local akey = require('awful.key')
|
||||||
local akeygrabber = require('awful.keygrabber')
|
local akeygrabber = require('awful.keygrabber')
|
||||||
local aplacement = require('awful.placement')
|
|
||||||
local apopup = require('awful.popup')
|
|
||||||
local base = require('wibox.widget.base')
|
|
||||||
local beautiful = require('beautiful')
|
|
||||||
local cairo = require('lgi').cairo
|
local cairo = require('lgi').cairo
|
||||||
local gobject = require('gears.object')
|
local gobject = require('gears.object')
|
||||||
local gstring = require('gears.string')
|
|
||||||
local gsurface = require('gears.surface')
|
|
||||||
local gtable = require('gears.table')
|
local gtable = require('gears.table')
|
||||||
local gtimer = require('gears.timer')
|
local gtimer = require('gears.timer')
|
||||||
local imagebox = require('wibox.widget.imagebox')
|
local imagebox = require('wibox.widget.imagebox')
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local textbox = require('wibox.widget.textbox')
|
|
||||||
local wibox = require('wibox')
|
|
||||||
local dpi = beautiful.xresources.apply_dpi
|
|
||||||
|
|
||||||
local capi = {
|
local capi = {
|
||||||
selection = selection,
|
selection = selection,
|
||||||
@@ -30,7 +20,7 @@ local inputbox = {}
|
|||||||
local function get_subtext_layout(layout, starti, endi)
|
local function get_subtext_layout(layout, starti, endi)
|
||||||
local text = layout:get_text()
|
local text = layout:get_text()
|
||||||
|
|
||||||
local subtext = text:sub(starti, endi)
|
local subtext = text:sub(starti + 1, endi)
|
||||||
|
|
||||||
local ctx = layout:get_context()
|
local ctx = layout:get_context()
|
||||||
|
|
||||||
@@ -54,6 +44,8 @@ function inputbox.draw_text(self)
|
|||||||
--local cairo_text = self._private.text_hint
|
--local cairo_text = self._private.text_hint
|
||||||
-- Get the text extents from Pango so we don't need to use cairo to get a possibly wrong extent
|
-- Get the text extents from Pango so we don't need to use cairo to get a possibly wrong extent
|
||||||
-- Then draw the text with cairo
|
-- Then draw the text with cairo
|
||||||
|
|
||||||
|
--return
|
||||||
end
|
end
|
||||||
|
|
||||||
local _, pango_extent = self._private.layout:get_extents()
|
local _, pango_extent = self._private.layout:get_extents()
|
||||||
@@ -110,21 +102,26 @@ function inputbox.draw_text(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function inputbox:start_keygrabber()
|
function inputbox:start_keygrabber()
|
||||||
self._private.cursor_timer = gtimer {
|
|
||||||
timeout = 1,
|
|
||||||
autostart = true,
|
|
||||||
callback = function()
|
|
||||||
self._private.show_cursor = not self._private.show_cursor
|
|
||||||
self:draw_text()
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
self._private.akeygrabber = akeygrabber {
|
self._private.akeygrabber = akeygrabber {
|
||||||
autostart = true,
|
autostart = true,
|
||||||
stop_key = {},
|
stop_key = {},
|
||||||
|
mask_event_callback = false,
|
||||||
start_callback = function(_, mod, key)
|
start_callback = function(_, mod, key)
|
||||||
|
self._private.cursor_timer = gtimer {
|
||||||
|
timeout = 1,
|
||||||
|
autostart = true,
|
||||||
|
callback = function()
|
||||||
|
self._private.show_cursor = not self._private.show_cursor
|
||||||
|
self:draw_text()
|
||||||
|
end,
|
||||||
|
}
|
||||||
self:emit_signal('inputbox::start', mod, key)
|
self:emit_signal('inputbox::start', mod, key)
|
||||||
end,
|
end,
|
||||||
stop_callback = function(_, mod, key)
|
stop_callback = function(_, mod, key)
|
||||||
|
self._private.cursor_timer:stop()
|
||||||
|
self._private.show_cursor = false
|
||||||
|
self._private.cursor_timer = nil
|
||||||
|
self:draw_text()
|
||||||
self:emit_signal('inputbox::stop', mod, key)
|
self:emit_signal('inputbox::stop', mod, key)
|
||||||
end,
|
end,
|
||||||
keybindings = {
|
keybindings = {
|
||||||
@@ -134,11 +131,12 @@ function inputbox:start_keygrabber()
|
|||||||
on_press = function()
|
on_press = function()
|
||||||
local hl = self:get_highlight()
|
local hl = self:get_highlight()
|
||||||
local text = self:get_text()
|
local text = self:get_text()
|
||||||
|
if text == '' then return end
|
||||||
local cursor_pos = self:get_cursor_index()
|
local cursor_pos = self:get_cursor_index()
|
||||||
if hl.end_pos ~= hl.start_pos then
|
if hl.end_pos ~= hl.start_pos then
|
||||||
self:set_text(text:sub(0, hl.start_pos) .. text:sub(hl.end_pos + 1, #text))
|
self:set_text(text:sub(0, hl.start_pos) .. text:sub(hl.end_pos + 1, #text))
|
||||||
self:set_cursor_pos(hl.start_pos)
|
self:set_cursor_pos(hl.start_pos)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
else
|
else
|
||||||
self:set_text(text:sub(1, cursor_pos - 1) .. text:sub(cursor_pos + 1))
|
self:set_text(text:sub(1, cursor_pos - 1) .. text:sub(cursor_pos + 1))
|
||||||
self:set_cursor_pos(cursor_pos - 1)
|
self:set_cursor_pos(cursor_pos - 1)
|
||||||
@@ -156,7 +154,7 @@ function inputbox:start_keygrabber()
|
|||||||
if hl.end_pos ~= hl.start_pos then
|
if hl.end_pos ~= hl.start_pos then
|
||||||
self:set_text(text:sub(0, hl.start_pos) .. text:sub(hl.end_pos + 1, #text))
|
self:set_text(text:sub(0, hl.start_pos) .. text:sub(hl.end_pos + 1, #text))
|
||||||
self:set_cursor_pos(hl.start_pos)
|
self:set_cursor_pos(hl.start_pos)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
else
|
else
|
||||||
self:set_text(text:sub(1, cursor_pos) .. text:sub(cursor_pos + 2, #text))
|
self:set_text(text:sub(1, cursor_pos) .. text:sub(cursor_pos + 2, #text))
|
||||||
end
|
end
|
||||||
@@ -168,7 +166,7 @@ function inputbox:start_keygrabber()
|
|||||||
key = 'Left',
|
key = 'Left',
|
||||||
on_press = function()
|
on_press = function()
|
||||||
self:set_cursor_pos(self:get_cursor_index() - 1)
|
self:set_cursor_pos(self:get_cursor_index() - 1)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Move cursor to right
|
akey { -- Move cursor to right
|
||||||
@@ -176,7 +174,7 @@ function inputbox:start_keygrabber()
|
|||||||
key = 'Right',
|
key = 'Right',
|
||||||
on_press = function()
|
on_press = function()
|
||||||
self:set_cursor_pos(self:get_cursor_index() + 1)
|
self:set_cursor_pos(self:get_cursor_index() + 1)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Jump cursor to text beginning
|
akey { -- Jump cursor to text beginning
|
||||||
@@ -184,7 +182,7 @@ function inputbox:start_keygrabber()
|
|||||||
key = 'Home',
|
key = 'Home',
|
||||||
on_press = function()
|
on_press = function()
|
||||||
self:set_cursor_pos(0)
|
self:set_cursor_pos(0)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Jump cursor to text end
|
akey { -- Jump cursor to text end
|
||||||
@@ -192,7 +190,7 @@ function inputbox:start_keygrabber()
|
|||||||
key = 'End',
|
key = 'End',
|
||||||
on_press = function()
|
on_press = function()
|
||||||
self:set_cursor_pos(#self:get_text())
|
self:set_cursor_pos(#self:get_text())
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Highlight to the left
|
akey { -- Highlight to the left
|
||||||
@@ -201,20 +199,20 @@ function inputbox:start_keygrabber()
|
|||||||
on_press = function()
|
on_press = function()
|
||||||
local cursor_pos = self:get_cursor_index()
|
local cursor_pos = self:get_cursor_index()
|
||||||
local hl = self:get_highlight()
|
local hl = self:get_highlight()
|
||||||
if cursor_pos == hl.start_pos then
|
|
||||||
self:set_cursor_pos(cursor_pos - 1)
|
if hl.end_pos == 0 and hl.start_pos == 0 then
|
||||||
self:set_highlight { start_pos = self:get_cursor_index(), end_pos = hl.end_pos }
|
self:set_highlight(cursor_pos, cursor_pos)
|
||||||
elseif cursor_pos == hl.end_pos then
|
|
||||||
self:set_cursor_pos(cursor_pos - 1)
|
|
||||||
self:set_highlight { start_pos = hl.start_pos, end_pos = self:get_cursor_index() }
|
|
||||||
else
|
|
||||||
if (hl.start_pos ~= cursor_pos) and (hl.end_pos ~= cursor_pos) then
|
|
||||||
self:set_highlight { start_pos = cursor_pos, end_pos = cursor_pos }
|
|
||||||
hl = self:get_highlight()
|
|
||||||
self:set_cursor_pos(cursor_pos - 1)
|
|
||||||
self:set_highlight { start_pos = self:get_cursor_index(), end_pos = hl.end_pos }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if hl.start_pos >= cursor_pos then
|
||||||
|
self:set_cursor_pos(cursor_pos - 1)
|
||||||
|
self:set_highlight(hl.start_pos - 1, nil)
|
||||||
|
else
|
||||||
|
self:set_cursor_pos(cursor_pos - 1)
|
||||||
|
self:set_highlight(nil, hl.end_pos - 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
print('cursor_pos', self:get_cursor_index(), 'hl.start_pos', self:get_highlight().start_pos, 'hl.end_pos', self:get_highlight().end_pos)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Highlight to the right
|
akey { -- Highlight to the right
|
||||||
@@ -225,16 +223,16 @@ function inputbox:start_keygrabber()
|
|||||||
local hl = self:get_highlight()
|
local hl = self:get_highlight()
|
||||||
if cursor_pos == hl.end_pos then
|
if cursor_pos == hl.end_pos then
|
||||||
self:set_cursor_pos(cursor_pos + 1)
|
self:set_cursor_pos(cursor_pos + 1)
|
||||||
self:set_highlight { start_pos = hl.start_pos, end_pos = self:get_cursor_index() }
|
self:set_highlight(hl.start_pos, self:get_cursor_index())
|
||||||
elseif cursor_pos == hl.start_pos then
|
elseif cursor_pos == hl.start_pos then
|
||||||
self:set_cursor_pos(cursor_pos + 1)
|
self:set_cursor_pos(cursor_pos + 1)
|
||||||
self:set_highlight { start_pos = self:get_cursor_index(), end_pos = hl.end_pos }
|
self:set_highlight(self:get_cursor_index(), hl.end_pos)
|
||||||
else
|
else
|
||||||
if (hl.start_pos ~= cursor_pos) and (hl.end_pos ~= cursor_pos) then
|
if (hl.start_pos ~= cursor_pos) and (hl.end_pos ~= cursor_pos) then
|
||||||
self:set_highlight { start_pos = cursor_pos, end_pos = cursor_pos }
|
self:set_highlight(cursor_pos, cursor_pos)
|
||||||
hl = self:get_highlight()
|
hl = self:get_highlight()
|
||||||
self:set_cursor_pos(cursor_pos + 1)
|
self:set_cursor_pos(cursor_pos + 1)
|
||||||
self:set_highlight { start_pos = hl.start_pos, end_pos = self:get_cursor_index() }
|
self:set_highlight(hl.start_pos, self:get_cursor_index())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
@@ -243,8 +241,8 @@ function inputbox:start_keygrabber()
|
|||||||
modifiers = { 'Control' },
|
modifiers = { 'Control' },
|
||||||
key = 'a',
|
key = 'a',
|
||||||
on_press = function()
|
on_press = function()
|
||||||
self:set_highlight { start_pos = 0, end_pos = #self:get_text() }
|
self:set_highlight(0, #self:get_text())
|
||||||
self:set_cursor_pos(#self:get_text() - 1)
|
self:set_cursor_pos(#self:get_text())
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
akey { -- Copy highlight
|
akey { -- Copy highlight
|
||||||
@@ -267,7 +265,7 @@ function inputbox:start_keygrabber()
|
|||||||
if hl.start_pos ~= hl.end_pos then
|
if hl.start_pos ~= hl.end_pos then
|
||||||
self:set_text(self:get_text():sub(1, hl.start_pos) .. selection .. self:get_text():sub(hl.end_pos + 1, #self:get_text()))
|
self:set_text(self:get_text():sub(1, hl.start_pos) .. selection .. self:get_text():sub(hl.end_pos + 1, #self:get_text()))
|
||||||
self:set_cursor_pos(hl.start_pos + #selection)
|
self:set_cursor_pos(hl.start_pos + #selection)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
else
|
else
|
||||||
self:set_text(self:get_text():sub(1, self:get_cursor_pos()) .. selection .. self:get_text():sub(self:get_cursor_pos() + 1, #self:get_text()))
|
self:set_text(self:get_text():sub(1, self:get_cursor_pos()) .. selection .. self:get_text():sub(self:get_cursor_pos() + 1, #self:get_text()))
|
||||||
self:set_cursor_pos(self:get_cursor_pos() + #selection)
|
self:set_cursor_pos(self:get_cursor_pos() + #selection)
|
||||||
@@ -315,15 +313,21 @@ function inputbox:start_keygrabber()
|
|||||||
self._private.show_cursor = true
|
self._private.show_cursor = true
|
||||||
local text = self:get_text()
|
local text = self:get_text()
|
||||||
local cursor_pos = self:get_cursor_index()
|
local cursor_pos = self:get_cursor_index()
|
||||||
if (mod[1] == 'Mod2' or '') and (key:wlen() == 1) then
|
if (mod[1] == 'Mod2' or '') and (key:wlen() == 1) and (not (mod[1] == 'Control')) then
|
||||||
self:set_text(text:sub(1, cursor_pos) .. key .. text:sub(cursor_pos + 1, #text))
|
self:set_text(text:sub(1, cursor_pos) .. key .. text:sub(cursor_pos + 1, #text))
|
||||||
self:set_cursor_pos(cursor_pos + #key)
|
self:set_cursor_pos(cursor_pos + #key)
|
||||||
elseif (mod[1] == 'Shift') and (key:wlen() == 1) then
|
elseif (mod[1] == 'Shift') and (key:wlen() == 1) then
|
||||||
self:set_text(text:sub(1, cursor_pos) .. key:upper() .. text:sub(cursor_pos + 1, #text))
|
self:set_text(text:sub(1, cursor_pos) .. key:upper() .. text:sub(cursor_pos + 1, #text))
|
||||||
self:set_cursor_pos(cursor_pos + #key)
|
self:set_cursor_pos(cursor_pos + #key)
|
||||||
end
|
end
|
||||||
|
self.draw_text(self)
|
||||||
self:emit_signal('inputbox::keypressed', mod, key)
|
self:emit_signal('inputbox::keypressed', mod, key)
|
||||||
end,
|
end,
|
||||||
|
keyreleased_callback = function(_, mod, key)
|
||||||
|
self._private.cursor_timer:start()
|
||||||
|
self.draw_text(self)
|
||||||
|
self:emit_signal('inputbox::keyreleased', mod, key)
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -366,7 +370,7 @@ function inputbox:start_mousegrabber(x, y)
|
|||||||
|
|
||||||
self:set_cursor_pos(index)
|
self:set_cursor_pos(index)
|
||||||
-- Remove highlight, but also prepare its position (same pos = no highlight)
|
-- Remove highlight, but also prepare its position (same pos = no highlight)
|
||||||
self:set_highlight { start_pos = index, end_pos = index }
|
self:set_highlight(index, index)
|
||||||
|
|
||||||
local text = self:get_text()
|
local text = self:get_text()
|
||||||
local hl = self:get_highlight()
|
local hl = self:get_highlight()
|
||||||
@@ -382,18 +386,18 @@ function inputbox:start_mousegrabber(x, y)
|
|||||||
if mb_start - index == 1 then
|
if mb_start - index == 1 then
|
||||||
if index <= hl.start_pos then
|
if index <= hl.start_pos then
|
||||||
if hl.end_pos < #text then
|
if hl.end_pos < #text then
|
||||||
self:set_highlight { start_pos = hl.start_pos, end_pos = hl.end_pos + 1 }
|
self:set_highlight(hl.start_pos, hl.end_pos + 1)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
self:set_highlight { start_pos = hl.start_pos + 1, end_pos = hl.end_pos }
|
self:set_highlight(hl.start_pos + 1, hl.end_pos)
|
||||||
end
|
end
|
||||||
elseif mb_start - index == -1 then
|
elseif mb_start - index == -1 then
|
||||||
if index >= hl.end_pos then
|
if index >= hl.end_pos then
|
||||||
if hl.start_pos > 1 then
|
if hl.start_pos > 1 then
|
||||||
self:set_highlight { start_pos = hl.start_pos - 1, end_pos = hl.end_pos }
|
self:set_highlight(hl.start_pos - 1, hl.end_pos)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
self:set_highlight { start_pos = hl.start_pos, end_pos = hl.end_pos - 1 }
|
self:set_highlight(hl.start_pos, hl.end_pos - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -413,7 +417,7 @@ function inputbox:set_cursor_pos_from_mouse(x, y)
|
|||||||
local index = self._private.layout:xy_to_index(x * Pango.SCALE, y * Pango.SCALE)
|
local index = self._private.layout:xy_to_index(x * Pango.SCALE, y * Pango.SCALE)
|
||||||
if not index then index = #self:get_text() end
|
if not index then index = #self:get_text() end
|
||||||
|
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
self:set_cursor_pos(index)
|
self:set_cursor_pos(index)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -464,14 +468,30 @@ function inputbox:get_highlight()
|
|||||||
return self._private.highlight
|
return self._private.highlight
|
||||||
end
|
end
|
||||||
|
|
||||||
function inputbox:set_highlight(highlight)
|
function inputbox:set_highlight(start_pos, end_pos)
|
||||||
self._private.highlight = highlight
|
if start_pos then
|
||||||
|
self._private.highlight.start_pos = start_pos
|
||||||
|
end
|
||||||
|
if end_pos then
|
||||||
|
self._private.highlight.end_pos = end_pos
|
||||||
|
end
|
||||||
|
|
||||||
self.draw_text(self)
|
self.draw_text(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local button_signal = function(self, x, y, button)
|
||||||
|
if button == 1 then
|
||||||
|
self:set_cursor_pos_from_mouse(x, y)
|
||||||
|
self:start_mousegrabber(x, y)
|
||||||
|
self:start_keygrabber()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function inputbox:focus()
|
function inputbox:focus()
|
||||||
self._private.show_cursor = true
|
self._private.show_cursor = true
|
||||||
self:start_keygrabber()
|
self:start_keygrabber()
|
||||||
|
self.widget:connect_signal('button::press', button_signal)
|
||||||
|
self.draw_text(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function inputbox:unfocus(reset)
|
function inputbox:unfocus(reset)
|
||||||
@@ -481,8 +501,9 @@ function inputbox:unfocus(reset)
|
|||||||
end
|
end
|
||||||
if reset then
|
if reset then
|
||||||
self:set_cursor_pos(0)
|
self:set_cursor_pos(0)
|
||||||
self:set_highlight { start_pos = 0, end_pos = 0 }
|
self:set_highlight(0, 0)
|
||||||
end
|
end
|
||||||
|
self.widget:disconnect_signal('button::press', button_signal)
|
||||||
self._private.show_cursor = false
|
self._private.show_cursor = false
|
||||||
self.draw_text(self)
|
self.draw_text(self)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user