From d8fe49931798dd054d79e17f9fb3371168d800ca Mon Sep 17 00:00:00 2001 From: Daniyil Glushko Date: Sun, 2 Jul 2023 19:10:33 +0300 Subject: [PATCH] initial background widget code --- extra/config.toml | 13 +++++++++++++ src/config.rs | 13 +++++++++++++ src/ui/background.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/ui/mod.rs | 11 ++++++++++- 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/ui/background.rs diff --git a/extra/config.toml b/extra/config.toml index 8577a37..d3096d3 100644 --- a/extra/config.toml +++ b/extra/config.toml @@ -81,6 +81,19 @@ shell_login_flag = "short" # - password: Initially focus on the password field focus_behaviour = "default" +# General settings for background style +[background] + +# Control whether to render background widget or not +show_background = false + +[background.style] +# Allow to set the default background color for the login shell +color = "black" +# Settings for the background block's borders +show_border = true +border_color = "white" + [power_controls] # Allow for the shutdown option to be used allow_shutdown = true diff --git a/src/config.rs b/src/config.rs index d263797..f3b7b9d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -175,12 +175,25 @@ toml_config_struct! { Config, PartialConfig, focus_behaviour => FocusBehaviour, + background => BackgroundConfig [PartialBackgroundConfig], + power_controls => PowerControlConfig [PartialPowerControlConfig], environment_switcher => SwitcherConfig [PartialSwitcherConfig], username_field => UsernameFieldConfig [PartialUsernameFieldConfig], password_field => PasswordFieldConfig [PartialPasswordFieldConfig], } +toml_config_struct! { BackgroundStyleConfig, PartialBackgroundStyleConfig, + color => String, + show_border => bool, + border_color => String, +} + +toml_config_struct! { BackgroundConfig, PartialBackgroundConfig, + show_background => bool, + style => BackgroundStyleConfig [PartialBackgroundStyleConfig], +} + toml_config_struct! { PowerControlConfig, PartialPowerControlConfig, allow_shutdown => bool, shutdown_hint => String, diff --git a/src/ui/background.rs b/src/ui/background.rs new file mode 100644 index 0000000..bd5d8e3 --- /dev/null +++ b/src/ui/background.rs @@ -0,0 +1,44 @@ +use ratatui::{ + style::Style, + widgets::{Block, Borders}, + Frame, +}; + +use crate::config::{get_color, BackgroundConfig}; + +#[derive(Clone)] +pub struct BackgroundWidget { + config: BackgroundConfig, +} + +impl BackgroundWidget { + pub fn new(config: BackgroundConfig) -> Self { + Self { config } + } + + pub fn render(&self, frame: &mut Frame) { + if !self.config.show_background { + return; + } + let block = Block::default().style(self.background_style()); + + let bounding_box = frame.size(); + + let block = if self.config.style.show_border { + block + .borders(Borders::ALL) + .border_style(self.border_style()) + } else { + block + }; + + frame.render_widget(block, bounding_box); + } + + fn background_style(&self) -> Style { + Style::default().bg(get_color(&self.config.style.color)) + } + fn border_style(&self) -> Style { + Style::default().fg(get_color(&self.config.style.border_color)) + } +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 4257bf3..bb4323d 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -20,6 +20,7 @@ use crossterm::terminal::{ use ratatui::backend::CrosstermBackend; use ratatui::{backend::Backend, Frame, Terminal}; +mod background; mod chunks; mod input_field; mod key_menu; @@ -32,6 +33,8 @@ use key_menu::KeyMenuWidget; use status_message::{ErrorStatusMessage, InfoStatusMessage}; use switcher::{SwitcherItem, SwitcherWidget}; +use self::background::BackgroundWidget; + #[derive(Clone)] struct LoginFormInputMode(Arc>); @@ -162,6 +165,7 @@ enum UIThreadRequest { #[derive(Clone)] struct Widgets { + background: BackgroundWidget, key_menu: KeyMenuWidget, environment: Arc>>, username: Arc>, @@ -280,6 +284,7 @@ impl LoginForm { LoginForm { preview, widgets: Widgets { + background: BackgroundWidget::new(config.background.clone()), key_menu: KeyMenuWidget::new( config.power_controls.clone(), config.environment_switcher.clone(), @@ -341,7 +346,7 @@ impl LoginForm { FocusBehaviour::Password => InputMode::Password, }); let status_message = LoginFormStatusMessage::new(); - + let background = self.widgets.background.clone(); let key_menu = self.widgets.key_menu.clone(); let environment = self.widgets.environment.clone(); let username = self.widgets.username.clone(); @@ -352,6 +357,7 @@ impl LoginForm { login_form_render( f, layout, + background.clone(), key_menu.clone(), environment.clone(), username.clone(), @@ -555,6 +561,7 @@ impl LoginForm { login_form_render( f, layout, + background.clone(), key_menu.clone(), environment.clone(), username.clone(), @@ -595,6 +602,7 @@ impl LoginForm { fn login_form_render( frame: &mut Frame, chunks: Chunks, + background: BackgroundWidget, key_menu: KeyMenuWidget, environment: Arc>>, username: Arc>, @@ -602,6 +610,7 @@ fn login_form_render( input_mode: InputMode, status_message: Option, ) { + background.render(frame); key_menu.render(frame, chunks.key_menu); environment .lock()