refactor: move fixed_window to ui

This is a ui module

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-06-24 17:11:01 -07:00
parent b14a2bf254
commit 1228f83e50
4 changed files with 5 additions and 8 deletions

62
src/ui/fixed_window.rs Normal file
View File

@@ -0,0 +1,62 @@
use egui::{Rect, Response, RichText, Sense, Window};
pub struct FixedWindow {
title: Option<RichText>,
}
#[derive(PartialEq)]
pub enum FixedWindowResponse {
Opened,
Closed,
}
impl FixedWindow {
#[allow(dead_code)]
pub fn new() -> Self {
Self { title: None }
}
pub fn maybe_with_title(maybe_title: Option<RichText>) -> Self {
Self { title: maybe_title }
}
#[allow(dead_code)]
pub fn with_title(mut self, title: RichText) -> Self {
self.title = Some(title);
self
}
pub fn show(
self,
ui: &mut egui::Ui,
rect: Rect,
add_contents: impl FnOnce(&mut egui::Ui) -> Response,
) -> FixedWindowResponse {
let mut is_open = true;
let use_title_bar = self.title.is_some();
let title = if let Some(title) = self.title {
title
} else {
RichText::new("")
};
Window::new(title)
.open(&mut is_open)
.fixed_rect(rect)
.collapsible(false)
.movable(false)
.resizable(false)
.title_bar(use_title_bar)
.show(ui.ctx(), |ui| {
let resp = add_contents(ui);
ui.allocate_rect(resp.rect, Sense::hover())
});
if !is_open {
FixedWindowResponse::Closed
} else {
FixedWindowResponse::Opened
}
}
}

View File

@@ -3,11 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use egui::Sense;
use egui_nav::{Nav, NavAction};
use crate::{
fixed_window::{FixedWindow, FixedWindowResponse},
route::Route,
Damus,
};
use crate::{route::Route, ui, Damus};
static MARGIN: f32 = 200.0;
@@ -25,7 +21,7 @@ impl DesktopGlobalPopup {
let app_ctx = Rc::new(RefCell::new(app));
let resp = FixedWindow::maybe_with_title(title).show(ui, rect, |ui| {
let resp = ui::FixedWindow::maybe_with_title(title).show(ui, rect, |ui| {
let nav_response =
Nav::new(routes)
.title(false)
@@ -49,7 +45,7 @@ impl DesktopGlobalPopup {
let mut app = app_ctx.borrow_mut();
if resp == FixedWindowResponse::Closed {
if resp == ui::FixedWindowResponse::Closed {
app.global_nav.pop();
app.show_global_popup = false;
}

View File

@@ -2,6 +2,7 @@ pub mod account_login_view;
pub mod account_management;
pub mod account_switcher;
pub mod anim;
pub mod fixed_window;
pub mod global_popup;
pub mod mention;
pub mod note;
@@ -13,6 +14,7 @@ pub mod username;
pub use account_management::AccountManagementView;
pub use account_switcher::AccountSelectionWidget;
pub use fixed_window::{FixedWindow, FixedWindowResponse};
pub use global_popup::DesktopGlobalPopup;
pub use mention::Mention;
pub use note::{BarAction, Note, NoteResponse, PostView};