use std::{error::Error, fmt, rc::Rc};
use slint::ComponentHandle;
#[cfg(not(target_os = "windows"))]
mod unsupported;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(not(target_os = "windows"))]
use unsupported as platform;
#[cfg(target_os = "windows")]
use windows as platform;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CaptionButton {
Minimize,
Maximize,
Close,
}
impl CaptionButton {
pub const fn slint_value(self) -> i32 {
match self {
Self::Minimize => 1,
Self::Maximize => 2,
Self::Close => 3,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FrameEvent {
Installed,
CaptionHoverChanged(Option<CaptionButton>),
Detached,
Failed(FrameError),
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct LogicalPoint {
pub x: f32,
pub y: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct LogicalSize {
pub width: f32,
pub height: f32,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HorizontalAnchor {
Left,
Right,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ClientArea {
pub anchor: HorizontalAnchor,
pub origin: LogicalPoint,
pub size: LogicalSize,
}
impl ClientArea {
pub const fn left(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
anchor: HorizontalAnchor::Left,
origin: LogicalPoint { x, y },
size: LogicalSize { width, height },
}
}
pub const fn right(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
anchor: HorizontalAnchor::Right,
origin: LogicalPoint { x, y },
size: LogicalSize { width, height },
}
}
}
#[derive(Clone, Debug)]
pub struct FrameOptions {
pub titlebar_height: f32,
pub caption_button_width: f32,
pub minimum_size: Option<LogicalSize>,
pub rounded_corners: bool,
pub client_areas: Vec<ClientArea>,
}
impl Default for FrameOptions {
fn default() -> Self {
Self {
titlebar_height: 48.0,
caption_button_width: 46.0,
minimum_size: None,
rounded_corners: true,
client_areas: Vec::new(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FrameError {
UnsupportedPlatform,
RenderingNotifierUnavailable(String),
WindowHandleUnavailable(String),
NotAWin32Window,
InvalidOptions,
AlreadyAttached,
NativeSubclassFailed,
NativeDetachFailed,
}
impl fmt::Display for FrameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsupportedPlatform => f.write_str("slint-borderless only supports Windows"),
Self::RenderingNotifierUnavailable(error) => {
write!(f, "Slint rendering notifier is unavailable: {error}")
}
Self::WindowHandleUnavailable(error) => {
write!(f, "native window handle is unavailable: {error}")
}
Self::NotAWin32Window => f.write_str("Slint did not create a Win32 window"),
Self::InvalidOptions => f.write_str("frame dimensions must be finite and positive"),
Self::AlreadyAttached => f.write_str("a frame is already attached"),
Self::NativeSubclassFailed => f.write_str("SetWindowSubclass failed"),
Self::NativeDetachFailed => f.write_str("RemoveWindowSubclass failed"),
}
}
}
impl Error for FrameError {}
#[derive(Clone)]
pub struct FrameController {
inner: Rc<platform::FrameInner>,
}
impl FrameController {
pub fn attach<C: ComponentHandle + 'static>(
component: &C,
options: FrameOptions,
) -> Result<Self, FrameError> {
platform::attach(component, options).map(|inner| Self { inner })
}
pub fn attach_window(
window: &slint::Window,
options: FrameOptions,
) -> Result<Self, FrameError> {
platform::attach_window(window, options).map(|inner| Self { inner })
}
pub fn on_event(&self, handler: impl Fn(FrameEvent) + 'static) {
self.inner.set_event_handler(Some(Rc::new(handler)));
}
pub fn clear_event_handler(&self) {
self.inner.set_event_handler(None);
}
pub fn set_client_areas(&self, areas: impl Into<Vec<ClientArea>>) {
self.inner.set_client_areas(areas.into());
}
pub fn caption_hover(&self) -> Option<CaptionButton> {
self.inner.caption_hover()
}
pub fn is_attached(&self) -> bool {
self.inner.is_attached()
}
pub fn last_error(&self) -> Option<FrameError> {
self.inner.last_error()
}
pub fn detach(&self) -> Result<(), FrameError> {
self.inner.detach()
}
}
pub(crate) type EventHandler = Rc<dyn Fn(FrameEvent)>;
pub const SLINT_LIBRARY_NAME: &str = "slint-borderless";
pub fn slint_library_path() -> std::path::PathBuf {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui")
}