1
1
mirror of https://github.com/NixOS/mobile-nixos.git synced 2024-12-11 09:04:01 +03:00

lvgui: Extract battery

This commit is contained in:
Samuel Dionne-Riel 2021-06-08 21:37:32 -04:00
parent 90f629e309
commit ff9464c947
2 changed files with 43 additions and 44 deletions

View File

@ -0,0 +1,43 @@
# Implements a battery widget as a wrapped LVLabel.
class LVGUI::Battery < LVGUI::Widget
def initialize(parent)
super(LVGL::LVLabel.new(parent))
set_align(LVGL::LABEL_ALIGN::RIGHT)
set_long_mode(LVGL::LABEL_LONG::CROP)
@battery = LVGUI::HAL::Battery.main_battery
# Update the text once
update_text
# Then register a task to update regularly.
@task = LVGL::Hacks::LVTask.create_task(1000 * 15, LVGL::TASK_PRIO::LOW, ->() do
update_text
end)
end
def update_text()
if @battery
symbol =
if @battery.charging? then
LVGL::Symbols::CHARGE
elsif @battery.percent == "unknown"
""
elsif @battery.percent > 95
LVGL::Symbols::BATTERY_FULL
elsif @battery.percent > 75
LVGL::Symbols::BATTERY_3
elsif @battery.percent > 45
LVGL::Symbols::BATTERY_2
elsif @battery.percent > 10
LVGL::Symbols::BATTERY_1
else
LVGL::Symbols::BATTERY_EMPTY
end
set_text("#{symbol} #{@battery.percent}%")
else
set_text("")
end
end
end

View File

@ -218,50 +218,6 @@ module LVGUI
end
end
# Implements a battery widget as a wrapped LVLabel.
class Battery < Widget
def initialize(parent)
super(LVGL::LVLabel.new(parent))
set_align(LVGL::LABEL_ALIGN::RIGHT)
set_long_mode(LVGL::LABEL_LONG::CROP)
@battery = HAL::Battery.main_battery
# Update the text once
update_text
# Then register a task to update regularly.
@task = LVGL::Hacks::LVTask.create_task(1000 * 15, LVGL::TASK_PRIO::LOW, ->() do
update_text
end)
end
def update_text()
if @battery
symbol =
if @battery.charging? then
LVGL::Symbols::CHARGE
elsif @battery.percent == "unknown"
""
elsif @battery.percent > 95
LVGL::Symbols::BATTERY_FULL
elsif @battery.percent > 75
LVGL::Symbols::BATTERY_3
elsif @battery.percent > 45
LVGL::Symbols::BATTERY_2
elsif @battery.percent > 10
LVGL::Symbols::BATTERY_1
else
LVGL::Symbols::BATTERY_EMPTY
end
set_text("#{symbol} #{@battery.percent}%")
else
set_text("")
end
end
end
# Widget implementing the topmost status bar
class StatusBar < Widget
def initialize(parent)