Scroll to top on sheet listing changed (e.g. sorted)

This commit is contained in:
Julian Mutter 2024-05-25 22:01:42 +02:00
parent cfb3cc9835
commit 58901dae37
2 changed files with 31 additions and 15 deletions

View File

@ -5,6 +5,7 @@ use gtk::prelude::*;
use log::debug; use log::debug;
use relm4::{ use relm4::{
component::{AsyncComponent, AsyncComponentParts}, component::{AsyncComponent, AsyncComponentParts},
gtk::Adjustment,
prelude::*, prelude::*,
AsyncComponentSender, AsyncComponentSender,
}; };
@ -18,7 +19,7 @@ use crate::{
use super::{ use super::{
mcdu::McduModel, mcdu::McduModel,
sheet_listing::{SheetListingInput, SheetListingModel}, sheet_listing::{SheetListingInput, SheetListingModel, SheetListingOutput},
}; };
pub struct AppModel { pub struct AppModel {
@ -27,6 +28,7 @@ pub struct AppModel {
mcdu: Controller<McduModel>, mcdu: Controller<McduModel>,
sheets_listing: Controller<SheetListingModel>, sheets_listing: Controller<SheetListingModel>,
edit_mode: bool, edit_mode: bool,
scroll_adjustment: Adjustment,
} }
#[derive(Debug)] #[derive(Debug)]
@ -37,6 +39,7 @@ pub enum AppInput {
Sort, Sort,
Shuffle, Shuffle,
SetEditMode(bool), SetEditMode(bool),
SheetListingContentsChanged,
} }
pub struct AppInitData { pub struct AppInitData {
@ -94,6 +97,7 @@ impl AsyncComponent for AppModel {
model.sheets_listing.widget(), model.sheets_listing.widget(),
set_vexpand: true, set_vexpand: true,
set_hexpand: true, set_hexpand: true,
set_vadjustment: Some(&model.scroll_adjustment),
}, },
}, },
model.mcdu.widget() { model.mcdu.widget() {
@ -119,11 +123,13 @@ impl AsyncComponent for AppModel {
let mut sheets = init_data.sheets; let mut sheets = init_data.sheets;
sheets.sort_by(|a, b| a.cmp(b).reverse()); sheets.sort_by(|a, b| a.cmp(b).reverse());
let sheets_listing = SheetListingModel::builder() let sheets_listing = SheetListingModel::builder().launch(sheets).forward(
.launch(sheets) sender.input_sender(),
.forward(sender.input_sender(), |response| { |response| match response {
AppInput::SheetPressed(response.sheet) SheetListingOutput::SheetModelSelected(sheet) => AppInput::SheetPressed(sheet),
}); SheetListingOutput::ContentsChanged => AppInput::SheetListingContentsChanged,
},
);
let model = AppModel { let model = AppModel {
database: Arc::new(init_data.database), database: Arc::new(init_data.database),
@ -131,6 +137,7 @@ impl AsyncComponent for AppModel {
mcdu, mcdu,
sheets_listing, sheets_listing,
edit_mode: false, edit_mode: false,
scroll_adjustment: Adjustment::builder().build(),
}; };
let widgets = view_output!(); let widgets = view_output!();
@ -166,11 +173,12 @@ impl AsyncComponent for AppModel {
let dir = Arc::clone(&self.directory); let dir = Arc::clone(&self.directory);
sender.oneshot_command(async move { sender.oneshot_command(async move {
sheet_validation::load_and_validate_sheets(&db, dir.as_ref()).await sheet_validation::load_and_validate_sheets(&db, dir.as_ref()).await
}) });
} }
AppInput::Sort => self.sheets_listing.emit(SheetListingInput::Sort), AppInput::Sort => self.sheets_listing.emit(SheetListingInput::Sort),
AppInput::Shuffle => self.sheets_listing.emit(SheetListingInput::Shuffle), AppInput::Shuffle => self.sheets_listing.emit(SheetListingInput::Shuffle),
AppInput::SetEditMode(edit_mode) => self.edit_mode = edit_mode, AppInput::SetEditMode(edit_mode) => self.edit_mode = edit_mode,
AppInput::SheetListingContentsChanged => self.scroll_adjustment.set_value(0.0),
} }
} }

View File

@ -24,15 +24,16 @@ pub enum SheetListingInput {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct SheetModelSelected { pub enum SheetListingOutput {
pub sheet: Sheet, SheetModelSelected(Sheet),
ContentsChanged,
} }
#[relm4::component(pub)] #[relm4::component(pub)]
impl SimpleComponent for SheetListingModel { impl SimpleComponent for SheetListingModel {
type Init = Vec<Sheet>; type Init = Vec<Sheet>;
type Input = SheetListingInput; type Input = SheetListingInput;
type Output = SheetModelSelected; type Output = SheetListingOutput;
view! { view! {
#[root] #[root]
@ -74,18 +75,25 @@ impl SimpleComponent for SheetListingModel {
SheetListingInput::ListBoxRowClicked(index) => { SheetListingInput::ListBoxRowClicked(index) => {
let sheet_model = self.sheets.get(index as usize).unwrap(); let sheet_model = self.sheets.get(index as usize).unwrap();
sender sender
.output(SheetModelSelected { .output(SheetListingOutput::SheetModelSelected(
sheet: sheet_model.sheet.clone(), sheet_model.sheet.clone(),
}) ))
.unwrap(); .unwrap();
} }
SheetListingInput::Sort => sort_sheets(&mut self.sheets), SheetListingInput::Sort => {
SheetListingInput::Shuffle => shuffle_sheets(&mut self.sheets), sort_sheets(&mut self.sheets);
sender.output(SheetListingOutput::ContentsChanged);
}
SheetListingInput::Shuffle => {
shuffle_sheets(&mut self.sheets);
sender.output(SheetListingOutput::ContentsChanged);
}
SheetListingInput::ReloadSheets(sheets) => { SheetListingInput::ReloadSheets(sheets) => {
self.sheets.guard().clear(); self.sheets.guard().clear();
for sheet_model_type in sheets { for sheet_model_type in sheets {
self.sheets.guard().push_back(sheet_model_type); self.sheets.guard().push_back(sheet_model_type);
} }
sender.output(SheetListingOutput::ContentsChanged);
} }
} }
} }