Skip to content

Commit

Permalink
initial background widget code
Browse files Browse the repository at this point in the history
  • Loading branch information
danma3x authored and coastalwhite committed Jul 2, 2023
1 parent 458602e commit d8fe499
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
13 changes: 13 additions & 0 deletions extra/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions src/ui/background.rs
Original file line number Diff line number Diff line change
@@ -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<impl ratatui::backend::Backend>) {
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))
}
}
11 changes: 10 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mutex<InputMode>>);

Expand Down Expand Up @@ -162,6 +165,7 @@ enum UIThreadRequest {

#[derive(Clone)]
struct Widgets {
background: BackgroundWidget,
key_menu: KeyMenuWidget,
environment: Arc<Mutex<SwitcherWidget<PostLoginEnvironment>>>,
username: Arc<Mutex<InputFieldWidget>>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
Expand All @@ -352,6 +357,7 @@ impl LoginForm {
login_form_render(
f,
layout,
background.clone(),
key_menu.clone(),
environment.clone(),
username.clone(),
Expand Down Expand Up @@ -555,6 +561,7 @@ impl LoginForm {
login_form_render(
f,
layout,
background.clone(),
key_menu.clone(),
environment.clone(),
username.clone(),
Expand Down Expand Up @@ -595,13 +602,15 @@ impl LoginForm {
fn login_form_render<B: Backend>(
frame: &mut Frame<B>,
chunks: Chunks,
background: BackgroundWidget,
key_menu: KeyMenuWidget,
environment: Arc<Mutex<SwitcherWidget<PostLoginEnvironment>>>,
username: Arc<Mutex<InputFieldWidget>>,
password: Arc<Mutex<InputFieldWidget>>,
input_mode: InputMode,
status_message: Option<StatusMessage>,
) {
background.render(frame);
key_menu.render(frame, chunks.key_menu);
environment
.lock()
Expand Down

0 comments on commit d8fe499

Please sign in to comment.