aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/about/src/main.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2025-02-13 17:12:47 +0100
committerGitHub <noreply@github.com>2025-02-13 17:12:47 +0100
commit7aaa7aea8f9a378660492d03ff5736dbf846978d (patch)
tree2b9884e135d73c82da442d1de649eb2b5df3fc54 /default-plugins/about/src/main.rs
parentdocs(changelog): about plugin (diff)
downloadsl0ppij-7aaa7aea8f9a378660492d03ff5736dbf846978d.tar.gz
sl0ppij-7aaa7aea8f9a378660492d03ff5736dbf846978d.tar.xz
sl0ppij-7aaa7aea8f9a378660492d03ff5736dbf846978d.zip
feat(ux): tips on startup (#3988)
* add some tips * refactoring and max tip index * add config to disable release notes and startup tips * allow dismissing startup tips from plugin * random tip at startup * custom launch about * fix tests * style(fmt): rustfmt
Diffstat (limited to 'default-plugins/about/src/main.rs')
-rw-r--r--default-plugins/about/src/main.rs118
1 files changed, 110 insertions, 8 deletions
diff --git a/default-plugins/about/src/main.rs b/default-plugins/about/src/main.rs
index bd7c28ea..87af9ffd 100644
--- a/default-plugins/about/src/main.rs
+++ b/default-plugins/about/src/main.rs
@@ -1,11 +1,15 @@
mod active_component;
mod pages;
+mod tips;
use zellij_tile::prelude::*;
use pages::Page;
+use rand::prelude::*;
+use rand::rng;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
+use tips::MAX_TIP_INDEX;
const UI_ROWS: usize = 20;
const UI_COLUMNS: usize = 90;
@@ -20,6 +24,10 @@ struct App {
tab_columns: usize,
own_plugin_id: Option<u32>,
is_release_notes: bool,
+ is_startup_tip: bool,
+ tip_index: usize,
+ waiting_for_config_to_be_written: bool,
+ error: Option<String>,
}
impl Default for App {
@@ -32,6 +40,7 @@ impl Default for App {
link_executable.clone(),
"".to_owned(),
base_mode.clone(),
+ false,
),
link_executable,
zellij_version,
@@ -40,6 +49,10 @@ impl Default for App {
tab_columns: 0,
own_plugin_id: None,
is_release_notes: false,
+ is_startup_tip: false,
+ tip_index: 0,
+ waiting_for_config_to_be_written: false,
+ error: None,
}
}
}
@@ -52,27 +65,62 @@ impl ZellijPlugin for App {
.get("is_release_notes")
.map(|v| v == "true")
.unwrap_or(false);
+ self.is_startup_tip = configuration
+ .get("is_startup_tip")
+ .map(|v| v == "true")
+ .unwrap_or(false);
subscribe(&[
EventType::Key,
EventType::Mouse,
EventType::ModeUpdate,
EventType::RunCommandResult,
EventType::TabUpdate,
+ EventType::FailedToWriteConfigToDisk,
+ EventType::ConfigWasWrittenToDisk,
]);
let own_plugin_id = get_plugin_ids().plugin_id;
self.own_plugin_id = Some(own_plugin_id);
*self.zellij_version.borrow_mut() = get_zellij_version();
self.change_own_title();
self.query_link_executable();
- self.active_page = Page::new_main_screen(
- self.link_executable.clone(),
- self.zellij_version.borrow().clone(),
- self.base_mode.clone(),
- );
+ self.active_page = if self.is_startup_tip {
+ let mut rng = rng();
+ self.tip_index = rng.random_range(0..=MAX_TIP_INDEX);
+ Page::new_tip_screen(
+ self.link_executable.clone(),
+ self.base_mode.clone(),
+ self.tip_index,
+ )
+ } else {
+ Page::new_main_screen(
+ self.link_executable.clone(),
+ self.zellij_version.borrow().clone(),
+ self.base_mode.clone(),
+ self.is_release_notes,
+ )
+ };
}
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
+ Event::FailedToWriteConfigToDisk(file_path) => {
+ if self.waiting_for_config_to_be_written {
+ let error = match file_path {
+ Some(file_path) => {
+ format!("Failed to write config to disk at: {}", file_path)
+ },
+ None => format!("Failed to write config to disk."),
+ };
+ eprintln!("{}", error);
+ self.error = Some(error);
+ should_render = true;
+ }
+ },
+ Event::ConfigWasWrittenToDisk => {
+ if self.waiting_for_config_to_be_written {
+ close_self();
+ }
+ },
Event::TabUpdate(tab_info) => {
self.center_own_pane(tab_info);
},
@@ -98,14 +146,19 @@ impl ZellijPlugin for App {
}
},
Event::Key(key) => {
- should_render = self.handle_key(key);
+ if let Some(_error) = self.error.take() {
+ // dismiss error on any key
+ should_render = true;
+ } else {
+ should_render = self.handle_key(key);
+ }
},
_ => {},
}
should_render
}
fn render(&mut self, rows: usize, cols: usize) {
- self.active_page.render(rows, cols);
+ self.active_page.render(rows, cols, &self.error);
}
}
@@ -162,11 +215,24 @@ impl App {
}
pub fn handle_key(&mut self, key: KeyWithModifier) -> bool {
let mut should_render = false;
- if key.bare_key == BareKey::Enter && key.has_no_modifiers() {
+ if key.bare_key == BareKey::Up && key.has_no_modifiers() && self.is_startup_tip {
+ self.previous_tip();
+ should_render = true;
+ } else if key.bare_key == BareKey::Down && key.has_no_modifiers() && self.is_startup_tip {
+ self.next_tip();
+ should_render = true;
+ } else if key.bare_key == BareKey::Enter && key.has_no_modifiers() {
if let Some(new_page) = self.active_page.handle_selection() {
self.active_page = new_page;
should_render = true;
}
+ } else if key.bare_key == BareKey::Char('c')
+ && key.has_modifiers(&[KeyModifier::Ctrl])
+ && self.is_startup_tip
+ {
+ self.waiting_for_config_to_be_written = true;
+ let save_configuration = true;
+ reconfigure("show_startup_tips false".to_owned(), save_configuration);
} else if key.bare_key == BareKey::Esc && key.has_no_modifiers() {
if self.active_page.is_main_screen {
close_self();
@@ -175,9 +241,21 @@ impl App {
self.link_executable.clone(),
self.zellij_version.borrow().clone(),
self.base_mode.clone(),
+ self.is_release_notes,
);
should_render = true;
}
+ } else if key.bare_key == BareKey::Char('?')
+ && !self.is_release_notes
+ && !self.is_startup_tip
+ {
+ self.is_startup_tip = true;
+ self.active_page = Page::new_tip_screen(
+ self.link_executable.clone(),
+ self.base_mode.clone(),
+ self.tip_index,
+ );
+ should_render = true;
} else {
should_render = self.active_page.handle_key(key);
}
@@ -208,4 +286,28 @@ impl App {
}
}
}
+ fn previous_tip(&mut self) {
+ if self.tip_index == 0 {
+ self.tip_index = MAX_TIP_INDEX;
+ } else {
+ self.tip_index = self.tip_index.saturating_sub(1);
+ }
+ self.active_page = Page::new_tip_screen(
+ self.link_executable.clone(),
+ self.base_mode.clone(),
+ self.tip_index,
+ );
+ }
+ fn next_tip(&mut self) {
+ if self.tip_index == MAX_TIP_INDEX {
+ self.tip_index = 0;
+ } else {
+ self.tip_index += 1;
+ }
+ self.active_page = Page::new_tip_screen(
+ self.link_executable.clone(),
+ self.base_mode.clone(),
+ self.tip_index,
+ );
+ }
}