2018-01-13 22:38:53 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
|
|
local ButtonTable = require("ui/widget/buttontable")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local CloseButton = require("ui/widget/closebutton")
|
|
|
|
local Device = require("device")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
2020-05-09 11:16:09 +00:00
|
|
|
local MovableContainer = require("ui/widget/container/movablecontainer")
|
2018-01-13 22:38:53 +00:00
|
|
|
local NumberPickerWidget = require("ui/widget/numberpickerwidget")
|
2019-09-19 09:54:53 +00:00
|
|
|
local OverlapGroup = require("ui/widget/overlapgroup")
|
2018-01-13 22:38:53 +00:00
|
|
|
local Size = require("ui/size")
|
2019-01-15 17:38:23 +00:00
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
2018-01-13 22:38:53 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local Screen = Device.screen
|
|
|
|
|
|
|
|
local SpinWidget = InputContainer:new{
|
2020-05-09 11:16:09 +00:00
|
|
|
title_text = "",
|
2018-01-13 22:38:53 +00:00
|
|
|
title_face = Font:getFace("x_smalltfont"),
|
2020-05-09 11:16:09 +00:00
|
|
|
info_text = nil,
|
2020-06-12 23:56:36 +00:00
|
|
|
width = math.floor(Screen:getWidth() * 0.95),
|
2018-01-13 22:38:53 +00:00
|
|
|
height = Screen:getHeight(),
|
2019-09-23 22:24:45 +00:00
|
|
|
value_table = nil,
|
|
|
|
value_index = nil,
|
2018-01-13 22:38:53 +00:00
|
|
|
value = 1,
|
|
|
|
value_max = 20,
|
|
|
|
value_min = 0,
|
2018-08-11 20:47:33 +00:00
|
|
|
value_step = 1,
|
|
|
|
value_hold_step = 4,
|
2020-05-09 11:16:09 +00:00
|
|
|
cancel_text = _("Close"),
|
|
|
|
ok_text = _("Apply"),
|
2021-01-11 17:14:17 +00:00
|
|
|
cancel_callback = nil,
|
|
|
|
callback = nil,
|
|
|
|
close_callback = nil,
|
2020-05-09 11:16:09 +00:00
|
|
|
keep_shown_on_apply = false,
|
|
|
|
-- Set this to add default button that restores number to its default value
|
2019-07-24 12:31:20 +00:00
|
|
|
default_value = nil,
|
2019-07-29 09:48:07 +00:00
|
|
|
default_text = _("Use default"),
|
2020-05-09 11:16:09 +00:00
|
|
|
-- Optional extra button on bottom
|
|
|
|
extra_text = nil,
|
|
|
|
extra_callback = nil,
|
2018-01-13 22:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function SpinWidget:init()
|
|
|
|
self.medium_font_face = Font:getFace("ffont")
|
|
|
|
self.light_bar = {}
|
|
|
|
self.screen_width = Screen:getWidth()
|
|
|
|
self.screen_height = Screen:getHeight()
|
|
|
|
if Device:hasKeys() then
|
|
|
|
self.key_events = {
|
|
|
|
Close = { {"Back"}, doc = "close spin widget" }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
TapClose = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
w = self.screen_width,
|
|
|
|
h = self.screen_height,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
Tame some ButtonTable users into re-using Buttontable instances if possible (#7166)
* QuickDictLookup, ImageViewer, NumberPicker: Smarter `update` that will re-use most of the widget's layout instead of re-instantiating all the things.
* SpinWidget/DoubleSpinWidget: The NumberPicker change above renders a hack to preserve alpha on these widgets almost unnecessary. Also fixed said hack to also apply to the center, value button.
* Button: Don't re-instantiate the frame in setText/setIcon when unnecessary (e.g., no change at all, or no layout change).
* Button: Add a refresh method that repaints and refreshes a *specific* Button (provided it's been painted once) all on its lonesome.
* ConfigDialog: Free everything that's going to be re-instatiated on update
* A few more post #7118 fixes:
* SkimTo: Always flag the chapter nav buttons as vsync
* Button: Fix the highlight on rounded buttons when vsync is enabled (e.g., it's now entirely visible, instead of showing a weird inverted corner glitch).
* Some more heuristic tweaks in Menu/TouchMenu/Button/IconButton
* ButtonTable: fix the annoying rounding issue I'd noticed in #7054 ;).
* Enable dithering in TextBoxWidget (e.g., in the Wikipedia full view). This involved moving the HW dithering align fixup to base, where it always ought to have been ;).
* Switch a few widgets that were using "partial" on close to "ui", or, more rarely, "flashui". The intent being to limit "partial" purely to the Reader, because it has a latency cost when mixed with other refreshes, which happens often enough in UI ;).
* Minor documentation tweaks around UIManager's `setDirty` to reflect that change.
* ReaderFooter: Force a footer repaint on resume if it is visible (otherwise, just update it).
* ReaderBookmark: In the same vein, don't repaint an invisible footer on bookmark count changes.
2021-01-28 23:20:15 +00:00
|
|
|
|
|
|
|
-- Actually the widget layout
|
2018-01-13 22:38:53 +00:00
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:update()
|
|
|
|
local value_widget = NumberPickerWidget:new{
|
|
|
|
show_parent = self,
|
2020-06-12 23:56:36 +00:00
|
|
|
width = math.floor(self.screen_width * 0.2),
|
2018-01-13 22:38:53 +00:00
|
|
|
value = self.value,
|
2019-09-23 22:24:45 +00:00
|
|
|
value_table = self.value_table,
|
|
|
|
value_index = self.value_index,
|
2018-01-13 22:38:53 +00:00
|
|
|
value_min = self.value_min,
|
|
|
|
value_max = self.value_max,
|
2018-08-11 20:47:33 +00:00
|
|
|
value_step = self.value_step,
|
|
|
|
value_hold_step = self.value_hold_step,
|
2019-01-15 17:38:23 +00:00
|
|
|
precision = self.precision,
|
2018-01-13 22:38:53 +00:00
|
|
|
}
|
|
|
|
local value_group = HorizontalGroup:new{
|
|
|
|
align = "center",
|
|
|
|
value_widget,
|
|
|
|
}
|
|
|
|
|
2019-09-19 09:54:53 +00:00
|
|
|
local close_button = CloseButton:new{ window = self, padding_top = Size.margin.title, }
|
|
|
|
local btn_width = close_button:getSize().w + Size.padding.default * 2
|
2019-10-21 13:20:40 +00:00
|
|
|
|
2018-01-13 22:38:53 +00:00
|
|
|
local value_title = FrameContainer:new{
|
|
|
|
padding = Size.padding.default,
|
|
|
|
margin = Size.margin.title,
|
|
|
|
bordersize = 0,
|
|
|
|
TextWidget:new{
|
2019-10-21 13:20:40 +00:00
|
|
|
text = self.title_text,
|
|
|
|
max_width = self.width - btn_width,
|
2018-01-13 22:38:53 +00:00
|
|
|
face = self.title_face,
|
|
|
|
bold = true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
local value_line = LineWidget:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = Size.line.thick,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
local value_bar = OverlapGroup:new{
|
|
|
|
dimen = {
|
|
|
|
w = self.width,
|
|
|
|
h = value_title:getSize().h
|
|
|
|
},
|
|
|
|
value_title,
|
2019-09-19 09:54:53 +00:00
|
|
|
close_button,
|
2018-01-13 22:38:53 +00:00
|
|
|
}
|
|
|
|
local buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = self.cancel_text,
|
|
|
|
callback = function()
|
2019-09-01 19:30:19 +00:00
|
|
|
if self.cancel_callback then
|
2021-01-11 17:14:17 +00:00
|
|
|
self.cancel_callback()
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
2018-01-13 22:38:53 +00:00
|
|
|
self:onClose()
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = self.ok_text,
|
|
|
|
callback = function()
|
|
|
|
if self.callback then
|
2019-09-23 22:24:45 +00:00
|
|
|
self.value, self.value_index = value_widget:getValue()
|
2021-01-11 17:14:17 +00:00
|
|
|
self.callback(self)
|
2018-01-13 22:38:53 +00:00
|
|
|
end
|
2020-05-09 11:16:09 +00:00
|
|
|
if not self.keep_shown_on_apply then
|
|
|
|
self:onClose()
|
|
|
|
end
|
2018-01-13 22:38:53 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:31:20 +00:00
|
|
|
if self.default_value then
|
|
|
|
table.insert(buttons,{
|
|
|
|
{
|
2019-07-29 09:48:07 +00:00
|
|
|
text = self.default_text,
|
2019-07-24 12:31:20 +00:00
|
|
|
callback = function()
|
|
|
|
value_widget.value = self.default_value
|
|
|
|
value_widget:update()
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
2020-05-09 11:16:09 +00:00
|
|
|
if self.extra_text then
|
|
|
|
table.insert(buttons,{
|
|
|
|
{
|
|
|
|
text = self.extra_text,
|
|
|
|
callback = function()
|
|
|
|
if self.extra_callback then
|
|
|
|
self.value, self.value_index = value_widget:getValue()
|
|
|
|
self.extra_callback(self)
|
|
|
|
end
|
|
|
|
if not self.keep_shown_on_apply then -- assume extra wants it same as ok
|
2019-09-14 14:30:25 +00:00
|
|
|
self:onClose()
|
2020-05-09 11:16:09 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
2019-09-14 14:30:25 +00:00
|
|
|
end
|
2019-07-24 12:31:20 +00:00
|
|
|
|
2018-01-13 22:38:53 +00:00
|
|
|
local ok_cancel_buttons = ButtonTable:new{
|
|
|
|
width = self.width - 2*Size.padding.default,
|
|
|
|
buttons = buttons,
|
|
|
|
zero_sep = true,
|
|
|
|
show_parent = self,
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:38:23 +00:00
|
|
|
local vgroup = VerticalGroup:new{
|
|
|
|
align = "left",
|
|
|
|
value_bar,
|
|
|
|
value_line,
|
|
|
|
}
|
2020-05-09 11:16:09 +00:00
|
|
|
if self.info_text then
|
2019-01-15 17:38:23 +00:00
|
|
|
table.insert(vgroup, FrameContainer:new{
|
|
|
|
padding = Size.padding.default,
|
|
|
|
margin = Size.margin.small,
|
|
|
|
bordersize = 0,
|
|
|
|
TextBoxWidget:new{
|
2020-05-09 11:16:09 +00:00
|
|
|
text = self.info_text,
|
2019-01-15 17:38:23 +00:00
|
|
|
face = Font:getFace("x_smallinfofont"),
|
2020-06-12 23:56:36 +00:00
|
|
|
width = math.floor(self.width * 0.9),
|
2019-01-15 17:38:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
table.insert(vgroup, CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
2020-06-12 23:56:36 +00:00
|
|
|
h = value_group:getSize().h + math.floor(self.screen_height * 0.1),
|
2019-01-15 17:38:23 +00:00
|
|
|
},
|
|
|
|
value_group
|
|
|
|
})
|
|
|
|
table.insert(vgroup, CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = ok_cancel_buttons:getSize().h,
|
|
|
|
},
|
|
|
|
ok_cancel_buttons
|
|
|
|
})
|
2018-01-13 22:38:53 +00:00
|
|
|
self.spin_frame = FrameContainer:new{
|
|
|
|
radius = Size.radius.window,
|
|
|
|
padding = 0,
|
|
|
|
margin = 0,
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2019-01-15 17:38:23 +00:00
|
|
|
vgroup,
|
2018-01-13 22:38:53 +00:00
|
|
|
}
|
2020-05-09 11:16:09 +00:00
|
|
|
self.movable = MovableContainer:new{
|
|
|
|
self.spin_frame,
|
|
|
|
}
|
2018-01-13 22:38:53 +00:00
|
|
|
self[1] = WidgetContainer:new{
|
|
|
|
align = "center",
|
|
|
|
dimen =Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = self.screen_width,
|
|
|
|
h = self.screen_height,
|
|
|
|
},
|
2020-05-09 11:16:09 +00:00
|
|
|
self.movable,
|
2018-01-13 22:38:53 +00:00
|
|
|
}
|
Revamp flash_ui handling, once more, with feeling ;) (#7262)
* Simplify flash_ui handling (by handling the unhighlight pre-callback, c.f., #7262 for more details).
* UIManager: Handle translucent window-level widgets (and those wrapped in a translucent MovableContainer) properly in setDirty directly, making sure what's *underneath* them gets repainted to avoid alpha layering glitches. (This was previously handled via localized hacks).
* Update UIManager's documentation, and format it properly for ldoc parsing, making the HTML docs more useful.
* ReaderView: Reinitialize the various page areas when opening a new document, to prevent poisoning from the previous document.
* Event: Handle nils in an event's arguments.
* CheckButton/RadioButton: Switch to simple inversion to handle highlighting
* CheckButton: Make the highlight span the inner frame's width, instead of just the text's width, if possible.
* AlphaContainer: Fix & simplify, given the UIManager alpha handling.
* MovableContainer: When translucent, cache the canvas bb used for composition.
* Avoid spurious refreshes in a few widgets using various dummy *TextWidgets in order to first compute a text height.
* KeyValuePage: Avoid floats in size computations.
2021-02-20 17:22:48 +00:00
|
|
|
UIManager:setDirty(self, function()
|
|
|
|
return "ui", self.spin_frame.dimen
|
|
|
|
end)
|
2020-05-09 11:16:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:hasMoved()
|
|
|
|
local offset = self.movable:getMovedOffset()
|
|
|
|
return offset.x ~= 0 or offset.y ~= 0
|
2018-01-13 22:38:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:onCloseWidget()
|
|
|
|
UIManager:setDirty(nil, function()
|
Tame some ButtonTable users into re-using Buttontable instances if possible (#7166)
* QuickDictLookup, ImageViewer, NumberPicker: Smarter `update` that will re-use most of the widget's layout instead of re-instantiating all the things.
* SpinWidget/DoubleSpinWidget: The NumberPicker change above renders a hack to preserve alpha on these widgets almost unnecessary. Also fixed said hack to also apply to the center, value button.
* Button: Don't re-instantiate the frame in setText/setIcon when unnecessary (e.g., no change at all, or no layout change).
* Button: Add a refresh method that repaints and refreshes a *specific* Button (provided it's been painted once) all on its lonesome.
* ConfigDialog: Free everything that's going to be re-instatiated on update
* A few more post #7118 fixes:
* SkimTo: Always flag the chapter nav buttons as vsync
* Button: Fix the highlight on rounded buttons when vsync is enabled (e.g., it's now entirely visible, instead of showing a weird inverted corner glitch).
* Some more heuristic tweaks in Menu/TouchMenu/Button/IconButton
* ButtonTable: fix the annoying rounding issue I'd noticed in #7054 ;).
* Enable dithering in TextBoxWidget (e.g., in the Wikipedia full view). This involved moving the HW dithering align fixup to base, where it always ought to have been ;).
* Switch a few widgets that were using "partial" on close to "ui", or, more rarely, "flashui". The intent being to limit "partial" purely to the Reader, because it has a latency cost when mixed with other refreshes, which happens often enough in UI ;).
* Minor documentation tweaks around UIManager's `setDirty` to reflect that change.
* ReaderFooter: Force a footer repaint on resume if it is visible (otherwise, just update it).
* ReaderBookmark: In the same vein, don't repaint an invisible footer on bookmark count changes.
2021-01-28 23:20:15 +00:00
|
|
|
return "ui", self.spin_frame.dimen
|
2018-01-13 22:38:53 +00:00
|
|
|
end)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:onShow()
|
|
|
|
UIManager:setDirty(self, function()
|
|
|
|
return "ui", self.spin_frame.dimen
|
|
|
|
end)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:onAnyKeyPressed()
|
2021-01-11 17:14:17 +00:00
|
|
|
self:onClose()
|
2018-01-13 22:38:53 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:onTapClose(arg, ges_ev)
|
|
|
|
if ges_ev.pos:notIntersectWith(self.spin_frame.dimen) then
|
|
|
|
self:onClose()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function SpinWidget:onClose()
|
|
|
|
UIManager:close(self)
|
2021-01-11 17:14:17 +00:00
|
|
|
if self.close_callback then
|
|
|
|
self.close_callback()
|
|
|
|
end
|
2018-01-13 22:38:53 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return SpinWidget
|