Skip to content

Commit

Permalink
refactor: optional last tab page (flxzt#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
flxzt authored Sep 29, 2024
1 parent 7a5338a commit f20775c
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 358 deletions.
235 changes: 145 additions & 90 deletions crates/rnote-ui/src/appwindow/actions.rs

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions crates/rnote-ui/src/appwindow/appsettings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ impl RnAppWindow {

{
// Save engine config of the current active tab
self.active_tab_wrapper()
.canvas()
.save_engine_config(&app_settings)?;
if let Some(canvas) = self.active_tab_canvas() {
canvas.save_engine_config(&app_settings)?;
}
}

{
Expand Down Expand Up @@ -490,11 +490,10 @@ impl RnAppWindow {
#[upgrade_or]
glib::ControlFlow::Break,
move || {
if let Err(e) = appwindow
.active_tab_wrapper()
.canvas()
.save_engine_config(&app_settings)
{
let Some(canvas) = appwindow.active_tab_canvas() else {
return glib::ControlFlow::Continue;
};
if let Err(e) = canvas.save_engine_config(&app_settings) {
error!(
"Saving engine config in periodic save task failed , Err: {e:?}"
);
Expand Down
81 changes: 43 additions & 38 deletions crates/rnote-ui/src/appwindow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ impl RnAppWindow {
.penpicker()
.redo_button()
.set_sensitive(false);
self.refresh_ui_from_engine(&self.active_tab_wrapper());
if let Some(wrapper) = self.active_tab_wrapper() {
self.refresh_ui_from_engine(&wrapper);
}
}

fn setup_icon_theme(&self) {
Expand Down Expand Up @@ -210,7 +212,9 @@ impl RnAppWindow {
canvas.queue_resize();
}
if widget_flags.refresh_ui {
self.refresh_ui_from_engine(&self.active_tab_wrapper());
if let Some(wrapper) = self.active_tab_wrapper() {
self.refresh_ui_from_engine(&wrapper);
}
}
if widget_flags.store_modified {
canvas.set_unsaved_changes(true);
Expand Down Expand Up @@ -263,15 +267,8 @@ impl RnAppWindow {
}

/// Get the active (selected) tab page.
///
/// Panics if there is none, but this should never be the case,
/// since a first one is added initially and the UI hides closing the last tab.
pub(crate) fn active_tab_page(&self) -> adw::TabPage {
self.imp()
.overlays
.tabview()
.selected_page()
.expect("there must always be one active tab")
pub(crate) fn active_tab_page(&self) -> Option<adw::TabPage> {
self.imp().overlays.tabview().selected_page()
}

pub(crate) fn n_tabs_open(&self) -> usize {
Expand Down Expand Up @@ -302,11 +299,14 @@ impl RnAppWindow {
}

/// Get the active (selected) tab page child.
pub(crate) fn active_tab_wrapper(&self) -> RnCanvasWrapper {
pub(crate) fn active_tab_wrapper(&self) -> Option<RnCanvasWrapper> {
self.active_tab_page()
.child()
.downcast::<RnCanvasWrapper>()
.unwrap()
.map(|c| c.child().downcast::<RnCanvasWrapper>().unwrap())
}

/// Get the active (selected) tab page canvas.
pub(crate) fn active_tab_canvas(&self) -> Option<RnCanvas> {
self.active_tab_wrapper().map(|w| w.canvas())
}

/// adds the initial tab to the tabview
Expand All @@ -329,9 +329,8 @@ impl RnAppWindow {
pub(crate) fn new_canvas_wrapper(&self) -> RnCanvasWrapper {
let engine_config = self
.active_tab_wrapper()
.canvas()
.engine_ref()
.extract_engine_config();
.map(|w| w.canvas().engine_ref().extract_engine_config())
.unwrap_or_default();
let wrapper = RnCanvasWrapper::new();
let widget_flags = wrapper
.canvas()
Expand Down Expand Up @@ -465,9 +464,7 @@ impl RnAppWindow {
.close_page_finish(tab_page, confirm);
}

pub(crate) fn refresh_titles(&self, active_tab: &RnCanvasWrapper) {
let canvas = active_tab.canvas();

pub(crate) fn refresh_titles(&self, canvas: &RnCanvas) {
// Titles
let title = canvas.doc_title_display();
let subtitle = canvas.doc_folderpath_display();
Expand Down Expand Up @@ -531,7 +528,7 @@ impl RnAppWindow {
&self,
input_file: gio::File,
target_pos: Option<na::Vector2<f64>>,
rnote_file_new_tab: bool,
mut rnote_file_new_tab: bool,
) -> anyhow::Result<bool> {
let file_imported = match FileType::lookup_file_type(&input_file) {
FileType::RnoteFile => {
Expand All @@ -544,20 +541,16 @@ impl RnAppWindow {
self.overlays().tabview().set_selected_page(&page);
false
} else {
let rnote_file_new_tab = if self.active_tab_wrapper().canvas().empty()
&& self.active_tab_wrapper().canvas().output_file().is_none()
{
false
let wrapper = if let Some(wrapper) = self.active_tab_wrapper() {
wrapper
} else {
rnote_file_new_tab
};

let wrapper = if rnote_file_new_tab {
// a new tab for rnote files
rnote_file_new_tab = true;
self.new_canvas_wrapper()
} else {
self.active_tab_wrapper()
};
if !wrapper.canvas().empty() || wrapper.canvas().output_file().is_some() {
rnote_file_new_tab = true;
}

let (bytes, _) = input_file.load_bytes_future().await?;
let widget_flags = wrapper
.canvas()
Expand All @@ -571,15 +564,21 @@ impl RnAppWindow {
}
}
FileType::VectorImageFile => {
let canvas = self.active_tab_wrapper().canvas();
let canvas = self
.active_tab_wrapper()
.ok_or_else(|| anyhow::anyhow!("No active tab to import into"))?
.canvas();
let (bytes, _) = input_file.load_bytes_future().await?;
canvas
.load_in_vectorimage_bytes(bytes.to_vec(), target_pos, self.respect_borders())
.await?;
true
}
FileType::BitmapImageFile => {
let canvas = self.active_tab_wrapper().canvas();
let canvas = self
.active_tab_wrapper()
.ok_or_else(|| anyhow::anyhow!("No active tab to import into"))?
.canvas();
let (bytes, _) = input_file.load_bytes_future().await?;
canvas
.load_in_bitmapimage_bytes(bytes.to_vec(), target_pos, self.respect_borders())
Expand All @@ -598,12 +597,18 @@ impl RnAppWindow {
file_imported
}
FileType::PdfFile => {
let canvas = self.active_tab_wrapper().canvas();
let canvas = self
.active_tab_wrapper()
.ok_or_else(|| anyhow::anyhow!("No active tab to import into"))?
.canvas();
dialogs::import::dialog_import_pdf_w_prefs(self, &canvas, input_file, target_pos)
.await?
}
FileType::PlaintextFile => {
let canvas = self.active_tab_wrapper().canvas();
let canvas = self
.active_tab_wrapper()
.ok_or_else(|| anyhow::anyhow!("No active tab to import into"))?
.canvas();
let (bytes, _) = input_file.load_bytes_future().await?;
canvas.load_in_text(String::from_utf8(bytes.to_vec())?, target_pos)?;
true
Expand Down Expand Up @@ -873,7 +878,7 @@ impl RnAppWindow {
.tools_page()
.refresh_ui(active_tab);
self.sidebar().settings_panel().refresh_ui(active_tab);
self.refresh_titles(active_tab);
self.refresh_titles(&canvas);
}

/// Sync the state from the previous active tab and the current one. Used when the selected tab changes.
Expand Down
7 changes: 3 additions & 4 deletions crates/rnote-ui/src/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,7 @@ impl RnCanvas {
canvas.clear_output_file_watcher();
canvas.dismiss_output_file_modified_toast();
}

appwindow.refresh_titles(&appwindow.active_tab_wrapper());
appwindow.refresh_titles(canvas);
}
),
);
Expand Down Expand Up @@ -1181,8 +1180,8 @@ impl RnCanvas {
clone!(
#[weak]
appwindow,
move |_, _| {
appwindow.refresh_titles(&appwindow.active_tab_wrapper());
move |canvas, _| {
appwindow.refresh_titles(canvas);
}
),
);
Expand Down
12 changes: 9 additions & 3 deletions crates/rnote-ui/src/overlays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ impl RnOverlays {
#[weak]
appwindow,
move |colorpicker, _paramspec| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};
let stroke_color = colorpicker.stroke_color().into_compose_color();
let canvas = appwindow.active_tab_wrapper().canvas();
let current_pen_style =
canvas.engine_ref().penholder.current_pen_style_w_override();

Expand Down Expand Up @@ -196,8 +198,10 @@ impl RnOverlays {
#[weak]
appwindow,
move |colorpicker, _paramspec| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};
let fill_color = colorpicker.fill_color().into_compose_color();
let canvas = appwindow.active_tab_wrapper().canvas();
let stroke_style = canvas.engine_ref().penholder.current_pen_style_w_override();

match stroke_style {
Expand Down Expand Up @@ -232,7 +236,9 @@ impl RnOverlays {
#[weak]
appwindow,
move |_| {
let active_tab_page = appwindow.active_tab_page();
let Some(active_tab_page) = appwindow.active_tab_page() else {
return;
};
let active_canvaswrapper = active_tab_page
.child()
.downcast::<RnCanvasWrapper>()
Expand Down
66 changes: 32 additions & 34 deletions crates/rnote-ui/src/penssidebar/brushpage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ impl RnBrushPage {
#[weak]
appwindow,
move |picker, _| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};
let stroke_width = picker.stroke_width();
let canvas = appwindow.active_tab_wrapper().canvas();
let brush_style = canvas.engine_ref().pens_config.brush_config.style;

match brush_style {
Expand Down Expand Up @@ -268,21 +270,17 @@ impl RnBrushPage {
#[weak]
appwindow,
move |_, _| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};

if let Some(brush_style) = brushpage.brush_style() {
appwindow
.active_tab_wrapper()
.canvas()
.engine_mut()
.pens_config
.brush_config
.style = brush_style;
canvas.engine_mut().pens_config.brush_config.style = brush_style;
brushpage.stroke_width_picker().deselect_setters();

match brush_style {
BrushStyle::Marker => {
let stroke_width = appwindow
.active_tab_wrapper()
.canvas()
let stroke_width = canvas
.engine_mut()
.pens_config
.brush_config
Expand All @@ -298,9 +296,7 @@ impl RnBrushPage {
.set_icon_name("pen-brush-style-marker-symbolic");
}
BrushStyle::Solid => {
let stroke_width = appwindow
.active_tab_wrapper()
.canvas()
let stroke_width = canvas
.engine_mut()
.pens_config
.brush_config
Expand All @@ -316,9 +312,7 @@ impl RnBrushPage {
.set_icon_name("pen-brush-style-solid-symbolic");
}
BrushStyle::Textured => {
let stroke_width = appwindow
.active_tab_wrapper()
.canvas()
let stroke_width = canvas
.engine_mut()
.pens_config
.brush_config
Expand All @@ -345,14 +339,12 @@ impl RnBrushPage {
#[weak]
appwindow,
move |_, _| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};

if let Some(buildertype) = brushpage.buildertype() {
appwindow
.active_tab_wrapper()
.canvas()
.engine_mut()
.pens_config
.brush_config
.builder_type = buildertype;
canvas.engine_mut().pens_config.brush_config.builder_type = buildertype;
}
}
));
Expand All @@ -367,9 +359,11 @@ impl RnBrushPage {
#[weak]
appwindow,
move |_smoothstyle_pressure_curves_row| {
appwindow
.active_tab_wrapper()
.canvas()
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};

canvas
.engine_mut()
.pens_config
.brush_config
Expand All @@ -392,9 +386,11 @@ impl RnBrushPage {
#[weak]
appwindow,
move |row| {
appwindow
.active_tab_wrapper()
.canvas()
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};

canvas
.engine_mut()
.pens_config
.brush_config
Expand All @@ -411,10 +407,12 @@ impl RnBrushPage {
self,
#[weak]
appwindow,
move |_texturedstyle_distribution_row| {
appwindow
.active_tab_wrapper()
.canvas()
move |_| {
let Some(canvas) = appwindow.active_tab_canvas() else {
return;
};

canvas
.engine_mut()
.pens_config
.brush_config
Expand Down
Loading

0 comments on commit f20775c

Please sign in to comment.