use crate::{
application::{
constants::{TAB_HEADER_SIZE, TAB_PADDING},
error::ApplicationError,
Message, WindowType,
},
core::{
error::CoreError,
localisation::{Localisation, StringCache},
},
};
use core::fmt::Debug;
use i18n::utility::LanguageTag;
use iced::{
alignment::{Horizontal, Vertical},
widget::{Column, Container, Text},
window, Task, Element, Length, Renderer, Theme,
};
use std::{
any::Any,
path::PathBuf,
};
#[cfg(feature = "iced_aw")]
use crate::iced_aw::widgets::sidebar::TabLabel;
#[cfg(not(feature = "iced_aw"))]
use iced_aw::sidebar::TabLabel;
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
#[cfg(not(feature = "sync"))]
use std::rc::Rc as RefCount;
#[cfg(feature = "sync")]
#[cfg(target_has_atomic = "ptr")]
use std::sync::Arc as RefCount;
pub trait AnyWindowTrait: Any + WindowTrait {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub trait WindowTrait {
fn title<'a>(&'a self, string_cache: &'a StringCache) -> &String;
#[allow(unused_variables)]
fn try_update(
&mut self,
message: Message,
string_cache: &StringCache,
) -> Result<Task<Message>, ApplicationError> {
Ok(Task::none())
}
fn view<'a>(
&'a self,
id: window::Id,
localisation: &Localisation,
string_cache: &'a StringCache,
) -> Element<'_, Message, Theme, Renderer>;
fn scale_factor(&self) -> f64 {
1.0
}
fn window_type(&self) -> WindowType;
fn is_reusable(&self) -> bool {
false
}
fn is_global_disable(&self) -> bool {
false
}
#[allow(unused_variables)]
fn try_localise(
&mut self,
localisation: &Localisation,
) -> Result<(), ApplicationError> {
Ok(())
}
}
pub trait SaveDataTrait {
fn try_save(&mut self) -> Result<(), ApplicationError>;
fn is_unsaved(&self) -> bool;
fn name(&self) -> &str;
fn path(&self) -> Option<&PathBuf> {
None
}
}
pub trait AnyLocalisedTrait: Any + LocalisedTrait + Debug {
fn as_any(&self) -> &dyn Any;
}
pub trait LocalisedTrait {
#[allow(unused_variables)]
fn try_update(&mut self, localisation: &Localisation) -> Result<(), CoreError> {
Ok(())
}
fn title(&self) -> &String;
fn string(&self, index: usize) -> &String;
fn language_tag(&self) -> &RefCount<LanguageTag>;
}
pub trait TabTrait {
fn title<'a>(&self, string_cache: &'a StringCache) -> String;
fn tab_label<'a>(&self, string_cache: &'a StringCache) -> TabLabel {
TabLabel::Text(self.title(string_cache))
}
fn view<'a>(
&'a self,
id: window::Id,
localisation: &Localisation,
string_cache: &'a StringCache,
) -> Element<'_, Message, Theme, Renderer> {
let column = Column::new()
.spacing(20)
.push(Text::new(self.title(string_cache)).size(TAB_HEADER_SIZE))
.push(self.content(id, localisation, string_cache))
.align_x(iced::Alignment::Center);
Container::new(column)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.padding(TAB_PADDING)
.into()
}
fn content<'a>(
&'a self,
id: window::Id,
localisation: &Localisation,
string_cache: &'a StringCache,
) -> Element<'_, Message, Theme, Renderer>;
}