[UX] Adds DoubleSpinWidget (#5679)

Replaces HyphenationLimits widget by making it more generic.
reviewable/pr5684/r1
Robert 5 years ago committed by poire-z
parent 04d9a557aa
commit e26ad2b287

@ -1,3 +1,4 @@
local Device = require("device")
local Event = require("ui/event") local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer") local InputContainer = require("ui/widget/container/inputcontainer")
@ -9,6 +10,7 @@ local util = require("util")
local _ = require("gettext") local _ = require("gettext")
local C_ = _.pgettext local C_ = _.pgettext
local T = require("ffi/util").template local T = require("ffi/util").template
local Screen = Device.screen
local ReaderHyphenation = InputContainer:new{ local ReaderHyphenation = InputContainer:new{
hyph_menu_title = _("Hyphenation"), hyph_menu_title = _("Hyphenation"),
@ -32,15 +34,30 @@ function ReaderHyphenation:init()
return T(_("Left/right minimal sizes: %1"), limits_text) return T(_("Left/right minimal sizes: %1"), limits_text)
end, end,
callback = function() callback = function()
local HyphenationLimitsWidget = require("ui/widget/hyphenationlimits") local DoubleSpinWidget = require("/ui/widget/doublespinwidget")
local hyph_settings = self.hyph_algs_settings[self.hyph_alg] or {} local hyph_settings = self.hyph_algs_settings[self.hyph_alg] or {}
local alg_left_hyphen_min = hyph_settings.left_hyphen_min local alg_left_hyphen_min = hyph_settings.left_hyphen_min
local alg_right_hyphen_min = hyph_settings.right_hyphen_min local alg_right_hyphen_min = hyph_settings.right_hyphen_min
local hyph_limits_widget = HyphenationLimitsWidget:new{ local hyph_limits_widget = DoubleSpinWidget:new{
-- Min (1) and max (10) values are enforced by crengine
left_value = G_reader_settings:readSetting("hyph_left_hyphen_min") or alg_left_hyphen_min or 2, left_value = G_reader_settings:readSetting("hyph_left_hyphen_min") or alg_left_hyphen_min or 2,
left_min = 1,
left_max = 10,
right_value = G_reader_settings:readSetting("hyph_right_hyphen_min") or alg_right_hyphen_min or 2, right_value = G_reader_settings:readSetting("hyph_right_hyphen_min") or alg_right_hyphen_min or 2,
right_min = 1,
right_max = 10,
left_default = alg_left_hyphen_min or 2, left_default = alg_left_hyphen_min or 2,
right_default = alg_right_hyphen_min or 2, right_default = alg_right_hyphen_min or 2,
-- let room on the widget sides so we can see
-- the hyphenation changes happening
width = Screen:getWidth() * 0.6,
default_values = true,
default_text = _("Use language defaults"),
title_text = _("Hyphenation limits"),
info_text = _([[
Set minimum length before hyphenation occurs.
These settings will apply to all books with any hyphenation dictionary.
'Use language defaults' resets them.]]),
callback = function(left_hyphen_min, right_hyphen_min) callback = function(left_hyphen_min, right_hyphen_min)
G_reader_settings:saveSetting("hyph_left_hyphen_min", left_hyphen_min) G_reader_settings:saveSetting("hyph_left_hyphen_min", left_hyphen_min)
G_reader_settings:saveSetting("hyph_right_hyphen_min", right_hyphen_min) G_reader_settings:saveSetting("hyph_right_hyphen_min", right_hyphen_min)

@ -23,29 +23,32 @@ local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext") local _ = require("gettext")
local Screen = Device.screen local Screen = Device.screen
local HyphenationLimitsWidget = InputContainer:new{ local DoubleSpinWidget = InputContainer:new{
title_text = _("Hyphenation limits"), title_text = "",
title_face = Font:getFace("x_smalltfont"), title_face = Font:getFace("x_smalltfont"),
info_text = "",
width = nil, width = nil,
height = nil, height = nil,
-- Min (2) and max (10) values are enforced by crengine
left_min = 1, left_min = 1,
left_max = 10, left_max = 20,
left_value = 2, left_value = 1,
left_default = nil, left_default = nil,
left_text = _("Left"),
right_min = 1, right_min = 1,
right_max = 10, right_max = 20,
right_value = 2, right_value = 1,
right_default = nil, right_default = nil,
right_text = _("Right"),
-- set this to see extra default button
default_values = nil,
default_text = _("Use defaults"),
} }
function HyphenationLimitsWidget:init() function DoubleSpinWidget:init()
self.medium_font_face = Font:getFace("ffont") self.medium_font_face = Font:getFace("ffont")
self.screen_width = Screen:getWidth() self.screen_width = Screen:getWidth()
self.screen_height = Screen:getHeight() self.screen_height = Screen:getHeight()
-- let room on the widget sides so we can see self.width = self.width or self.screen_width * 0.8
-- the hyphenation changes happening
self.width = self.screen_width * 0.6
self.picker_width = self.screen_width * 0.25 self.picker_width = self.screen_width * 0.25
if Device:hasKeys() then if Device:hasKeys() then
self.key_events = { self.key_events = {
@ -63,12 +66,12 @@ function HyphenationLimitsWidget:init()
} }
}, },
}, },
} }
end end
self:update() self:update()
end end
function HyphenationLimitsWidget:update() function DoubleSpinWidget:update()
-- This picker_update_callback will be redefined later. It is needed -- This picker_update_callback will be redefined later. It is needed
-- so we can have our MovableContainer repainted on NumberPickerWidgets -- so we can have our MovableContainer repainted on NumberPickerWidgets
-- update It is needed if we have enabled transparency on MovableContainer, -- update It is needed if we have enabled transparency on MovableContainer,
@ -92,33 +95,44 @@ function HyphenationLimitsWidget:update()
wrap = false, wrap = false,
update_callback = function() picker_update_callback() end, update_callback = function() picker_update_callback() end,
} }
local hyph_group = HorizontalGroup:new{ local left_vertical_group = VerticalGroup:new{
align = "center", align = "center",
VerticalGroup:new{ VerticalSpan:new{ width = Size.span.vertical_large },
align = "center", TextWidget:new{
VerticalSpan:new{ width = Size.span.vertical_large }, text = self.left_text,
TextBoxWidget:new{ face = self.title_face,
text = _("Left"), max_width = 0.95 * self.width / 2,
alignment = "center",
face = self.title_face,
width = self.picker_width,
},
left_widget,
}, },
VerticalGroup:new{ left_widget,
align = "center", }
VerticalSpan:new{ width = Size.span.vertical_large }, local right_vertical_group = VerticalGroup:new{
TextBoxWidget:new{ align = "center",
text = _("Right"), VerticalSpan:new{ width = Size.span.vertical_large },
alignment = "center", TextWidget:new{
face = self.title_face, text = self.right_text,
width = self.picker_width, face = self.title_face,
max_width = 0.95 * self.width / 2,
},
right_widget,
}
local widget_group = HorizontalGroup:new{
align = "center",
CenterContainer:new{
dimen = Geom:new{
w = self.width / 2,
h = left_vertical_group:getSize().h,
}, },
right_widget, left_vertical_group
}, },
CenterContainer:new{
dimen = Geom:new{
w = self.width / 2,
h = right_vertical_group:getSize().h,
},
right_vertical_group
}
} }
local widget_title = FrameContainer:new{
local hyph_title = FrameContainer:new{
padding = Size.padding.default, padding = Size.padding.default,
margin = Size.margin.title, margin = Size.margin.title,
bordersize = 0, bordersize = 0,
@ -129,36 +143,30 @@ function HyphenationLimitsWidget:update()
width = self.width, width = self.width,
}, },
} }
local hyph_line = LineWidget:new{ local widget_line = LineWidget:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = self.width,
h = Size.line.thick, h = Size.line.thick,
} }
} }
local hyph_bar = OverlapGroup:new{ local widget_bar = OverlapGroup:new{
dimen = { dimen = {
w = self.width, w = self.width,
h = hyph_title:getSize().h h = widget_title:getSize().h
}, },
hyph_title, widget_title,
CloseButton:new{ window = self, padding_top = Size.margin.title, }, CloseButton:new{ window = self, padding_top = Size.margin.title, },
} }
local widget_info = FrameContainer:new{
local hyph_into_text = _([[
Set minimum length before hyphenation occurs.
These settings will apply to all books with any hyphenation dictionary.
'Use language defaults' resets them.]])
local hyph_info = FrameContainer:new{
padding = Size.padding.default, padding = Size.padding.default,
margin = Size.margin.small, margin = Size.margin.small,
bordersize = 0, bordersize = 0,
TextBoxWidget:new{ TextBoxWidget:new{
text = hyph_into_text, text = self.info_text,
face = Font:getFace("x_smallinfofont"), face = Font:getFace("x_smallinfofont"),
width = self.width * 0.9, width = self.width * 0.9,
} }
} }
local buttons = { local buttons = {
{ {
{ {
@ -176,9 +184,11 @@ These settings will apply to all books with any hyphenation dictionary.
end, end,
}, },
}, },
{ }
if self.default_values then
table.insert(buttons,{
{ {
text = _("Use language defaults"), text = self.default_text,
callback = function() callback = function()
left_widget.value = self.left_default left_widget.value = self.left_default
right_widget.value = self.right_default right_widget.value = self.right_default
@ -186,9 +196,9 @@ These settings will apply to all books with any hyphenation dictionary.
right_widget:update() right_widget:update()
self.callback(nil, nil) self.callback(nil, nil)
end, end,
}, }
} })
} end
local button_table = ButtonTable:new{ local button_table = ButtonTable:new{
width = self.width - 2*Size.padding.default, width = self.width - 2*Size.padding.default,
@ -197,23 +207,23 @@ These settings will apply to all books with any hyphenation dictionary.
show_parent = self, show_parent = self,
} }
self.hyph_frame = FrameContainer:new{ self.widget_frame = FrameContainer:new{
radius = Size.radius.window, radius = Size.radius.window,
padding = 0, padding = 0,
margin = 0, margin = 0,
background = Blitbuffer.COLOR_WHITE, background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new{ VerticalGroup:new{
align = "left", align = "left",
hyph_bar, widget_bar,
hyph_line, widget_line,
hyph_info, widget_info,
VerticalSpan:new{ width = Size.span.vertical_large }, VerticalSpan:new{ width = Size.span.vertical_large },
CenterContainer:new{ CenterContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = self.width,
h = hyph_group:getSize().h, h = widget_group:getSize().h,
}, },
hyph_group widget_group
}, },
VerticalSpan:new{ width = Size.span.vertical_large }, VerticalSpan:new{ width = Size.span.vertical_large },
CenterContainer:new{ CenterContainer:new{
@ -226,7 +236,7 @@ These settings will apply to all books with any hyphenation dictionary.
} }
} }
self.movable = MovableContainer:new{ self.movable = MovableContainer:new{
self.hyph_frame, self.widget_frame,
} }
self[1] = WidgetContainer:new{ self[1] = WidgetContainer:new{
align = "center", align = "center",
@ -238,7 +248,7 @@ These settings will apply to all books with any hyphenation dictionary.
self.movable, self.movable,
} }
UIManager:setDirty(self, function() UIManager:setDirty(self, function()
return "ui", self.hyph_frame.dimen return "ui", self.widget_frame.dimen
end) end)
picker_update_callback = function() picker_update_callback = function()
UIManager:setDirty("all", function() UIManager:setDirty("all", function()
@ -249,35 +259,35 @@ These settings will apply to all books with any hyphenation dictionary.
end end
end end
function HyphenationLimitsWidget:onCloseWidget() function DoubleSpinWidget:onCloseWidget()
UIManager:setDirty(nil, function() UIManager:setDirty(nil, function()
return "partial", self.hyph_frame.dimen return "partial", self.widget_frame.dimen
end) end)
return true return true
end end
function HyphenationLimitsWidget:onShow() function DoubleSpinWidget:onShow()
UIManager:setDirty(self, function() UIManager:setDirty(self, function()
return "ui", self.hyph_frame.dimen return "ui", self.widget_frame.dimen
end) end)
return true return true
end end
function HyphenationLimitsWidget:onAnyKeyPressed() function DoubleSpinWidget:onAnyKeyPressed()
UIManager:close(self) UIManager:close(self)
return true return true
end end
function HyphenationLimitsWidget:onTapClose(arg, ges_ev) function DoubleSpinWidget:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.hyph_frame.dimen) then if ges_ev.pos:notIntersectWith(self.widget_frame.dimen) then
self:onClose() self:onClose()
end end
return true return true
end end
function HyphenationLimitsWidget:onClose() function DoubleSpinWidget:onClose()
UIManager:close(self) UIManager:close(self)
return true return true
end end
return HyphenationLimitsWidget return DoubleSpinWidget
Loading…
Cancel
Save