use std::collections::HashMap;
use crate::support::point::{Point, Extent};
use crate::support::rect::Rect;
use crate::support::canvas::Canvas;
use crate::element::{ElementPtr, ViewLimits};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButtonKind {
Left,
Middle,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseButtonState {
Pressed,
Released,
}
#[derive(Debug, Clone, Copy)]
pub struct MouseButton {
pub down: bool,
pub click_count: i32,
pub button: MouseButtonKind,
pub modifiers: i32,
pub pos: Point,
}
impl MouseButton {
pub fn new(down: bool, button: MouseButtonKind, pos: Point) -> Self {
Self {
down,
click_count: 1,
button,
modifiers: 0,
pos,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
A, B, C, D, E, F, G, H, I, J, K, L, M,
N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
Key0, Key1, Key2, Key3, Key4,
Key5, Key6, Key7, Key8, Key9,
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
Up, Down, Left, Right,
Home, End, PageUp, PageDown,
Backspace, Delete, Insert,
Enter, Tab, Escape,
Space,
Shift, Control, Alt, Super,
LeftShift, RightShift,
LeftControl, RightControl,
LeftAlt, RightAlt,
LeftSuper, RightSuper,
CapsLock, NumLock, ScrollLock,
PrintScreen, Pause,
Menu,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyAction {
Press,
Release,
Repeat,
}
#[derive(Debug, Clone, Copy)]
pub struct KeyInfo {
pub key: KeyCode,
pub action: KeyAction,
pub modifiers: i32,
}
#[derive(Debug, Clone, Copy)]
pub struct TextInfo {
pub codepoint: char,
pub modifiers: i32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorTracking {
Entering,
Hovering,
Leaving,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CursorType {
#[default]
Arrow,
IBeam,
CrossHair,
Hand,
HResize,
VResize,
}
#[derive(Debug, Clone)]
pub struct DropInfo {
pub where_: Point,
pub data: HashMap<String, String>,
}
impl DropInfo {
pub fn new(pos: Point) -> Self {
Self {
where_: pos,
data: HashMap::new(),
}
}
}
pub mod modifiers {
pub const SHIFT: i32 = 1 << 0;
pub const CONTROL: i32 = 1 << 1;
pub const ALT: i32 = 1 << 2;
pub const SUPER: i32 = 1 << 3;
pub const CAPS_LOCK: i32 = 1 << 4;
pub const NUM_LOCK: i32 = 1 << 5;
#[cfg(target_os = "macos")]
pub const ACTION: i32 = SUPER;
#[cfg(not(target_os = "macos"))]
pub const ACTION: i32 = CONTROL;
}
pub trait BaseView {
fn draw(&mut self, canvas: &mut Canvas);
fn click(&mut self, btn: MouseButton);
fn drag(&mut self, btn: MouseButton);
fn cursor(&mut self, p: Point, status: CursorTracking);
fn scroll(&mut self, dir: Point, p: Point);
fn key(&mut self, k: KeyInfo) -> bool;
fn text(&mut self, info: TextInfo) -> bool;
fn begin_focus(&mut self);
fn end_focus(&mut self);
fn track_drop(&mut self, info: &DropInfo, status: CursorTracking);
fn drop(&mut self, info: &DropInfo) -> bool;
fn poll(&mut self);
}
pub struct View {
bounds: Rect,
cursor_pos: Point,
scale: f32,
content: Option<ElementPtr>,
is_focus: bool,
}
impl View {
pub fn new(size: Extent) -> Self {
Self {
bounds: Rect::from_origin_size(Point::zero(), size),
cursor_pos: Point::zero(),
scale: 1.0,
content: None,
is_focus: false,
}
}
pub fn bounds(&self) -> Rect {
self.bounds
}
pub fn size(&self) -> Extent {
self.bounds.size()
}
pub fn set_size(&mut self, size: Extent) {
self.bounds = Rect::from_origin_size(Point::zero(), size);
}
pub fn cursor_pos(&self) -> Point {
self.cursor_pos
}
pub fn scale(&self) -> f32 {
self.scale
}
pub fn set_scale(&mut self, scale: f32) {
self.scale = scale;
}
pub fn set_content(&mut self, content: ElementPtr) {
self.content = Some(content);
}
pub fn content(&self) -> Option<&ElementPtr> {
self.content.as_ref()
}
pub fn limits(&self) -> ViewLimits {
ViewLimits::full()
}
pub fn has_focus(&self) -> bool {
self.is_focus
}
pub fn refresh(&self) {
}
pub fn refresh_area(&self, area: Rect) {
}
}
impl BaseView for View {
fn draw(&mut self, canvas: &mut Canvas) {
if let Some(content) = &self.content {
}
}
fn click(&mut self, btn: MouseButton) {
}
fn drag(&mut self, btn: MouseButton) {
}
fn cursor(&mut self, p: Point, status: CursorTracking) {
self.cursor_pos = p;
}
fn scroll(&mut self, dir: Point, p: Point) {
}
fn key(&mut self, k: KeyInfo) -> bool {
false
}
fn text(&mut self, info: TextInfo) -> bool {
false
}
fn begin_focus(&mut self) {
self.is_focus = true;
}
fn end_focus(&mut self) {
self.is_focus = false;
}
fn track_drop(&mut self, info: &DropInfo, status: CursorTracking) {
}
fn drop(&mut self, info: &DropInfo) -> bool {
false
}
fn poll(&mut self) {
}
}
pub fn clipboard() -> String {
String::new()
}
pub fn set_clipboard(text: &str) {
}
pub fn set_cursor(cursor: CursorType) {
}
pub fn scroll_direction() -> Point {
Point::new(1.0, 1.0)
}