Skip to content

Commit

Permalink
Fix pages to have AppContext
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Dec 21, 2024
1 parent 377f3db commit 49e0816
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 208 deletions.
32 changes: 13 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::{path::PathBuf, rc::Rc, sync::Arc};
use tokio::spawn;

use crate::{
Expand Down Expand Up @@ -27,7 +27,7 @@ pub enum Notification {
Error(String),
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct AppContext {
pub config: Config,
pub env: Environment,
Expand All @@ -45,7 +45,7 @@ pub struct App {
pub page_stack: PageStack,
app_objects: AppObjects,
client: Option<Arc<Client>>,
ctx: AppContext,
ctx: Rc<AppContext>,
tx: Sender,

notification: Notification,
Expand All @@ -56,9 +56,10 @@ pub struct App {

impl App {
pub fn new(ctx: AppContext, tx: Sender, width: usize, height: usize) -> App {
let ctx = Rc::new(ctx);
App {
app_objects: AppObjects::default(),
page_stack: PageStack::new(ctx.theme.clone(), tx.clone()),
page_stack: PageStack::new(Rc::clone(&ctx), tx.clone()),
client: None,
ctx,
tx,
Expand Down Expand Up @@ -95,7 +96,7 @@ impl App {

let bucket_list_page = Page::of_bucket_list(
self.app_objects.get_bucket_items(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.pop(); // remove initializing page
Expand Down Expand Up @@ -145,8 +146,7 @@ impl App {
let object_list_page = Page::of_object_list(
current_object_items,
object_key,
self.ctx.config.ui.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(object_list_page);
Expand Down Expand Up @@ -179,8 +179,7 @@ impl App {
object_list_page.object_list(),
current_object_key,
object_list_page.list_state(),
self.ctx.config.ui.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(object_detail_page);
Expand All @@ -196,8 +195,7 @@ impl App {
let new_object_list_page = Page::of_object_list(
current_object_items,
object_key,
self.ctx.config.ui.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(new_object_list_page);
Expand Down Expand Up @@ -264,8 +262,7 @@ impl App {
let object_list_page = Page::of_object_list(
items,
current_object_key,
self.ctx.config.ui.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(object_list_page);
Expand Down Expand Up @@ -333,8 +330,7 @@ impl App {
object_page.object_list(),
map_key,
object_page.list_state(),
self.ctx.config.ui.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(object_detail_page);
Expand Down Expand Up @@ -404,7 +400,7 @@ impl App {
if helps.is_empty() {
return;
}
let help_page = Page::of_help(helps, self.ctx.theme.clone(), self.tx.clone());
let help_page = Page::of_help(helps, Rc::clone(&self.ctx), self.tx.clone());
self.page_stack.push(help_page);
}

Expand Down Expand Up @@ -527,9 +523,7 @@ impl App {
obj,
path.to_string_lossy().into(),
current_object_key,
self.ctx.config.preview.clone(),
self.ctx.env.clone(),
self.ctx.theme.clone(),
Rc::clone(&self.ctx),
self.tx.clone(),
);
self.page_stack.push(object_preview_page);
Expand Down
48 changes: 25 additions & 23 deletions src/pages/bucket_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cmp::Ordering;
use std::{cmp::Ordering, rc::Rc};

use laurier::{highlight::highlight_matched_text, key_code, key_code_char};
use ratatui::{
Expand All @@ -11,6 +11,7 @@ use ratatui::{
};

use crate::{
app::AppContext,
color::ColorTheme,
event::{AppEventType, Sender},
object::{BucketItem, ObjectKey},
Expand All @@ -32,7 +33,7 @@ pub struct BucketListPage {
filter_input_state: InputDialogState,
sort_dialog_state: BucketListSortDialogState,

theme: ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
}

Expand All @@ -45,7 +46,7 @@ enum ViewState {
}

impl BucketListPage {
pub fn new(bucket_items: Vec<BucketItem>, theme: ColorTheme, tx: Sender) -> Self {
pub fn new(bucket_items: Vec<BucketItem>, ctx: Rc<AppContext>, tx: Sender) -> Self {
let items_len = bucket_items.len();
let view_indices = (0..items_len).collect();
Self {
Expand All @@ -55,7 +56,7 @@ impl BucketListPage {
list_state: ScrollListState::new(items_len),
filter_input_state: InputDialogState::default(),
sort_dialog_state: BucketListSortDialogState::default(),
theme,
ctx,
tx,
}
}
Expand Down Expand Up @@ -174,33 +175,34 @@ impl BucketListPage {
&self.bucket_items,
&self.view_indices,
self.filter_input_state.input(),
&self.theme,
&self.ctx.theme,
offset,
selected,
area,
);

let list = ScrollList::new(list_items).theme(&self.theme);
let list = ScrollList::new(list_items).theme(&self.ctx.theme);
f.render_stateful_widget(list, area, &mut self.list_state);

if let ViewState::FilterDialog = self.view_state {
let filter_dialog = InputDialog::default()
.title("Filter")
.max_width(30)
.theme(&self.theme);
.theme(&self.ctx.theme);
f.render_stateful_widget(filter_dialog, area, &mut self.filter_input_state);

let (cursor_x, cursor_y) = self.filter_input_state.cursor();
f.set_cursor_position((cursor_x, cursor_y));
}

if let ViewState::SortDialog = self.view_state {
let sort_dialog = BucketListSortDialog::new(self.sort_dialog_state).theme(&self.theme);
let sort_dialog =
BucketListSortDialog::new(self.sort_dialog_state).theme(&self.ctx.theme);
f.render_widget(sort_dialog, area);
}

if let ViewState::CopyDetailDialog(state) = &mut self.view_state {
let copy_detail_dialog = CopyDetailDialog::default().theme(&self.theme);
let copy_detail_dialog = CopyDetailDialog::default().theme(&self.ctx.theme);
f.render_stateful_widget(copy_detail_dialog, area, state);
}
}
Expand Down Expand Up @@ -518,7 +520,7 @@ mod tests {

#[test]
fn test_render_without_scroll() -> std::io::Result<()> {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();
let mut terminal = setup_terminal()?;

Expand All @@ -527,7 +529,7 @@ mod tests {
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);
let area = Rect::new(0, 0, 30, 10);
page.render(f, area);
})?;
Expand Down Expand Up @@ -556,15 +558,15 @@ mod tests {

#[test]
fn test_render_with_scroll() -> std::io::Result<()> {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();
let mut terminal = setup_terminal()?;

terminal.draw(|f| {
let items = (0..16)
.map(|i| bucket_item(&format!("bucket{}", i + 1)))
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);
let area = Rect::new(0, 0, 30, 10);
page.render(f, area);
})?;
Expand Down Expand Up @@ -594,15 +596,15 @@ mod tests {

#[test]
fn test_render_filter_items() -> std::io::Result<()> {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();
let mut terminal = setup_terminal()?;

let items = ["foo", "bar", "baz", "qux", "foobar"]
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);
let area = Rect::new(0, 0, 30, 10);

page.handle_key(KeyEvent::from(KeyCode::Char('/')));
Expand Down Expand Up @@ -671,15 +673,15 @@ mod tests {

#[test]
fn test_render_sort_items() -> std::io::Result<()> {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();
let mut terminal = setup_terminal()?;

let items = ["foo", "bar", "baz", "qux", "foobar"]
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);
let area = Rect::new(0, 0, 30, 10);

page.handle_key(KeyEvent::from(KeyCode::Char('o')));
Expand Down Expand Up @@ -717,14 +719,14 @@ mod tests {

#[test]
fn test_filter_items() {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();

let items = ["foo", "bar", "baz", "qux", "foobar"]
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);

page.handle_key(KeyEvent::from(KeyCode::Char('/')));
page.handle_key(KeyEvent::from(KeyCode::Char('b')));
Expand Down Expand Up @@ -752,14 +754,14 @@ mod tests {

#[test]
fn test_sort_items() {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();

let items = ["foo", "bar", "baz", "qux", "foobar"]
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);

page.handle_key(KeyEvent::from(KeyCode::Char('o')));

Expand All @@ -784,14 +786,14 @@ mod tests {

#[test]
fn test_filter_and_sort_items() {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();

let items = ["foo", "bar", "baz", "qux", "foobar"]
.into_iter()
.map(bucket_item)
.collect();
let mut page = BucketListPage::new(items, theme, tx);
let mut page = BucketListPage::new(items, ctx, tx);

page.handle_key(KeyEvent::from(KeyCode::Char('/')));
page.handle_key(KeyEvent::from(KeyCode::Char('b')));
Expand Down
20 changes: 11 additions & 9 deletions src/pages/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use laurier::{key_code, key_code_char};
use ratatui::{
buffer::Buffer,
Expand All @@ -10,7 +12,7 @@ use ratatui::{
};

use crate::{
color::ColorTheme,
app::AppContext,
constant::{APP_DESCRIPTION, APP_HOMEPAGE, APP_NAME, APP_VERSION},
event::{AppEventType, Sender},
pages::util::build_short_helps,
Expand All @@ -22,13 +24,13 @@ use crate::{
pub struct HelpPage {
helps: Vec<String>,

theme: ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
}

impl HelpPage {
pub fn new(helps: Vec<String>, theme: ColorTheme, tx: Sender) -> Self {
Self { helps, theme, tx }
pub fn new(helps: Vec<String>, ctx: Rc<AppContext>, tx: Sender) -> Self {
Self { helps, ctx, tx }
}

pub fn handle_key(&mut self, key: KeyEvent) {
Expand All @@ -47,7 +49,7 @@ impl HelpPage {
let block = Block::bordered()
.padding(Padding::horizontal(1))
.title(APP_NAME)
.fg(self.theme.fg);
.fg(self.ctx.theme.fg);

let content_area = block.inner(area);

Expand All @@ -63,9 +65,9 @@ impl HelpPage {
APP_DESCRIPTION,
APP_VERSION,
APP_HOMEPAGE,
self.theme.link,
self.ctx.theme.link,
);
let divider = Divider::default().color(self.theme.divider);
let divider = Divider::default().color(self.ctx.theme.divider);
let help = Help::new(&self.helps);

f.render_widget(block, area);
Expand Down Expand Up @@ -187,7 +189,7 @@ mod tests {

#[test]
fn test_render() -> std::io::Result<()> {
let theme = ColorTheme::default();
let ctx = Rc::default();
let (tx, _) = event::new();
let mut terminal = setup_terminal()?;

Expand All @@ -201,7 +203,7 @@ mod tests {
.iter()
.map(|s| s.to_string())
.collect();
let mut page = HelpPage::new(helps, theme, tx);
let mut page = HelpPage::new(helps, ctx, tx);
let area = Rect::new(0, 0, 70, 20);
page.render(f, area);
})?;
Expand Down
Loading

0 comments on commit 49e0816

Please sign in to comment.