use std::path::PathBuf;
pub const EDITOR_INSET: f64 = 20.0;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Rect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
impl Rect {
#[must_use]
pub fn new(x: f64, y: f64, width: f64, height: f64) -> Rect {
Rect {
x,
y,
width,
height,
}
}
#[must_use]
pub fn contains(&self, other: &Rect) -> bool {
other.x >= self.x
&& other.y >= self.y
&& other.x + other.width <= self.x + self.width
&& other.y + other.height <= self.y + self.height
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Region {
pub display: u32,
pub rect: Rect,
}
impl Region {
#[must_use]
pub fn new(display: u32, rect: Rect) -> Region {
Region { display, rect }
}
#[must_use]
pub fn is_well_formed(&self) -> bool {
let Rect {
x,
y,
width,
height,
} = self.rect;
x.is_finite()
&& y.is_finite()
&& width.is_finite()
&& height.is_finite()
&& width > 0.0
&& height > 0.0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct EditorId(pub u64);
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Opened {
pub editor: EditorId,
pub width: f64,
pub height: f64,
}
impl Opened {
#[doc(hidden)]
#[must_use]
pub fn new(editor: EditorId, width: f64, height: f64) -> Opened {
Opened {
editor,
width,
height,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WindowKind {
Editor,
Menu,
Dialog,
Popup,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Window {
pub id: u64,
pub kind: WindowKind,
pub frame: Rect,
pub outside: bool,
}
impl Window {
#[doc(hidden)]
#[must_use]
pub fn new(id: u64, kind: WindowKind, frame: Rect, outside: bool) -> Window {
Window {
id,
kind,
frame,
outside,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Notice {
#[non_exhaustive]
Windows {
editor: EditorId,
windows: Vec<Window>,
},
#[non_exhaustive]
EditorClosed {
editor: EditorId,
slot: usize,
},
#[non_exhaustive]
Nested {
depth: u32,
},
#[non_exhaustive]
Cursor {
shape: CursorShape,
hidden: bool,
},
#[non_exhaustive]
MenuOpened {
menu: MenuId,
items: Vec<MenuItem>,
frame: Option<Rect>,
},
#[non_exhaustive]
MenuClosed {
menu: MenuId,
},
#[non_exhaustive]
PanelRequested {
panel: PanelId,
request: PanelRequest,
},
#[non_exhaustive]
PanelFinished {
panel: PanelId,
},
#[non_exhaustive]
OpenUrl {
url: String,
},
#[non_exhaustive]
RevealFiles {
paths: Vec<PathBuf>,
},
DisplayLost,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PanelId(pub u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PanelKind {
Open,
Save,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PanelAnswer {
pub panel: PanelId,
pub paths: Option<Vec<PathBuf>>,
}
impl PanelAnswer {
#[must_use]
pub fn new(panel: PanelId, paths: Option<Vec<PathBuf>>) -> PanelAnswer {
PanelAnswer { panel, paths }
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct PanelRequest {
pub kind: PanelKind,
pub directory: Option<PathBuf>,
pub name: String,
pub types: Vec<String>,
pub multiple: bool,
pub directories: bool,
pub files: bool,
pub packages: bool,
pub message: String,
}
impl PanelRequest {
#[doc(hidden)]
#[must_use]
pub fn new(
kind: PanelKind,
directory: Option<PathBuf>,
name: impl Into<String>,
types: Vec<String>,
message: impl Into<String>,
) -> PanelRequest {
PanelRequest {
kind,
directory,
name: name.into(),
types,
multiple: false,
directories: false,
files: true,
packages: true,
message: message.into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MenuId(pub u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MenuItemState {
Off,
On,
Mixed,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct MenuItem {
pub index: usize,
pub title: String,
pub enabled: bool,
pub state: MenuItemState,
pub separator: bool,
pub submenu: Vec<MenuItem>,
}
impl MenuItem {
#[doc(hidden)]
#[must_use]
pub fn new(
index: usize,
title: impl Into<String>,
enabled: bool,
state: MenuItemState,
separator: bool,
submenu: Vec<MenuItem>,
) -> MenuItem {
MenuItem {
index,
title: title.into(),
enabled,
state,
separator,
submenu,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MenuChoice {
pub menu: MenuId,
pub path: Vec<usize>,
}
impl MenuChoice {
#[must_use]
pub fn new(menu: MenuId, path: Vec<usize>) -> MenuChoice {
MenuChoice { menu, path }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct WindowMove {
pub editor: EditorId,
pub window: u64,
pub to: Point,
}
impl WindowMove {
#[must_use]
pub fn new(editor: EditorId, window: u64, to: Point) -> WindowMove {
WindowMove { editor, window, to }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WindowClose {
pub editor: EditorId,
pub window: u64,
}
impl WindowClose {
#[must_use]
pub fn new(editor: EditorId, window: u64) -> WindowClose {
WindowClose { editor, window }
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum CursorShape {
Arrow,
IBeam,
IBeamVertical,
PointingHand,
Crosshair,
OpenHand,
ClosedHand,
ResizeLeft,
ResizeRight,
ResizeLeftRight,
ResizeUp,
ResizeDown,
ResizeUpDown,
DragLink,
DragCopy,
OperationNotAllowed,
ContextualMenu,
DisappearingItem,
Image(CursorImage),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct CursorImage {
pub png: Vec<u8>,
pub width: f64,
pub height: f64,
pub hot_spot: Point,
}
impl CursorImage {
#[doc(hidden)]
#[must_use]
pub fn new(png: Vec<u8>, width: f64, height: f64, hot_spot: Point) -> CursorImage {
CursorImage {
png,
width,
height,
hot_spot,
}
}
}
impl Notice {
#[doc(hidden)]
#[must_use]
pub fn windows(editor: EditorId, windows: Vec<Window>) -> Notice {
Notice::Windows { editor, windows }
}
#[doc(hidden)]
#[must_use]
pub fn editor_closed(editor: EditorId, slot: usize) -> Notice {
Notice::EditorClosed { editor, slot }
}
#[doc(hidden)]
#[must_use]
pub fn nested(depth: u32) -> Notice {
Notice::Nested { depth }
}
#[doc(hidden)]
#[must_use]
pub fn cursor(shape: CursorShape, hidden: bool) -> Notice {
Notice::Cursor { shape, hidden }
}
#[doc(hidden)]
#[must_use]
pub fn menu_opened(menu: MenuId, items: Vec<MenuItem>, frame: Option<Rect>) -> Notice {
Notice::MenuOpened { menu, items, frame }
}
#[doc(hidden)]
#[must_use]
pub fn menu_closed(menu: MenuId) -> Notice {
Notice::MenuClosed { menu }
}
#[doc(hidden)]
#[must_use]
pub fn panel_requested(panel: PanelId, request: PanelRequest) -> Notice {
Notice::PanelRequested { panel, request }
}
#[doc(hidden)]
#[must_use]
pub fn panel_finished(panel: PanelId) -> Notice {
Notice::PanelFinished { panel }
}
#[doc(hidden)]
#[must_use]
pub fn open_url(url: impl Into<String>) -> Notice {
Notice::OpenUrl { url: url.into() }
}
#[doc(hidden)]
#[must_use]
pub fn reveal_files(paths: Vec<PathBuf>) -> Notice {
Notice::RevealFiles { paths }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
#[must_use]
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Modifiers: u32 {
const SHIFT = 1 << 0;
const CONTROL = 1 << 1;
const OPTION = 1 << 2;
const COMMAND = 1 << 3;
const CAPS_LOCK = 1 << 4;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PointerKind {
Down,
Up,
Move,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Button {
Left,
Right,
Middle,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WheelUnit {
Line,
Point,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct PointerInput {
pub editor: EditorId,
pub kind: PointerKind,
pub button: Button,
pub at: Point,
pub clicks: u32,
pub modifiers: Modifiers,
}
impl PointerInput {
#[must_use]
pub fn new(
editor: EditorId,
kind: PointerKind,
button: Button,
at: Point,
clicks: u32,
modifiers: Modifiers,
) -> PointerInput {
PointerInput {
editor,
kind,
button,
at,
clicks,
modifiers,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct MotionInput {
pub editor: EditorId,
pub dx: f64,
pub dy: f64,
pub modifiers: Modifiers,
}
impl MotionInput {
#[must_use]
pub fn new(editor: EditorId, dx: f64, dy: f64, modifiers: Modifiers) -> MotionInput {
MotionInput {
editor,
dx,
dy,
modifiers,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct WheelInput {
pub editor: EditorId,
pub at: Point,
pub dx: f64,
pub dy: f64,
pub unit: WheelUnit,
pub modifiers: Modifiers,
}
impl WheelInput {
#[must_use]
pub fn new(
editor: EditorId,
at: Point,
dx: f64,
dy: f64,
unit: WheelUnit,
modifiers: Modifiers,
) -> WheelInput {
WheelInput {
editor,
at,
dx,
dy,
unit,
modifiers,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct KeyInput {
pub editor: EditorId,
pub down: bool,
pub key_code: u16,
pub characters: String,
pub characters_ignoring_modifiers: String,
pub repeat: bool,
pub modifiers: Modifiers,
}
impl KeyInput {
#[must_use]
pub fn new(
editor: EditorId,
down: bool,
key_code: u16,
characters: impl Into<String>,
characters_ignoring_modifiers: impl Into<String>,
repeat: bool,
modifiers: Modifiers,
) -> KeyInput {
KeyInput {
editor,
down,
key_code,
characters: characters.into(),
characters_ignoring_modifiers: characters_ignoring_modifiers.into(),
repeat,
modifiers,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct TextInput {
pub editor: EditorId,
pub text: String,
}
impl TextInput {
#[must_use]
pub fn new(editor: EditorId, text: impl Into<String>) -> TextInput {
TextInput {
editor,
text: text.into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct ModifiersInput {
pub editor: EditorId,
pub modifiers: Modifiers,
}
impl ModifiersInput {
#[must_use]
pub fn new(editor: EditorId, modifiers: Modifiers) -> ModifiersInput {
ModifiersInput { editor, modifiers }
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Command {
Pointer(PointerInput),
Motion(MotionInput),
Wheel(WheelInput),
Key(KeyInput),
Text(TextInput),
Modifiers(ModifiersInput),
ReleaseAll(EditorId),
ChooseMenuItem(MenuChoice),
CloseMenu(MenuId),
AnswerPanel(PanelAnswer),
MoveWindow(WindowMove),
CloseWindow(WindowClose),
}