makara 0.2.6

A Bevy UI simplifier that make it easy to build GUI app with bevy engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Collection of built-in widgets provided by Makara.

pub mod button;
pub mod text;
pub mod row;
pub mod column;
pub mod modal;
pub mod checkbox;
pub mod root;
pub mod radio;
pub mod dropdown;
pub mod select;
pub mod link;
pub mod slider;
pub mod progress_bar;
pub mod circular;
pub mod tooltip;
pub mod text_input;
pub mod text_edit;
pub mod scroll;
pub mod image;

pub use button::*;
pub use text::*;
pub use row::*;
pub use column::*;
pub use modal::*;
pub use checkbox::*;
pub use root::*;
pub use radio::*;
pub use dropdown::*;
pub use select::*;
pub use link::*;
pub use slider::*;
pub use progress_bar::*;
pub use circular::*;
pub use tooltip::*;
pub use text_input::*;
pub use text_edit::*;
pub use scroll::*;
pub use image::*;

use bevy::prelude::*;
use bevy::window::{CursorIcon, SystemCursorIcon};
use bevy::ecs::query::QueryFilter;
use bevy::ecs::system::SystemParam;
use cosmic_text::{FontSystem, SwashCache, Attrs};
use crate::{events::*, colors::IntoColor};

pub trait Widget {
    fn build(self) -> impl Bundle;
}

/// Trait for setting tooltip and its style.
pub trait SetToolTip: Sized {
    fn set_tooltip(&mut self) -> &mut TooltipBundle;

    /// Enable and set tooltip set.
    fn tooltip(mut self, text: &str) -> Self {
        self.set_tooltip().use_tooltip = UseTooltip(true);
        self.set_tooltip().text = TooltipText(text.to_string());
        self.set_tooltip().text_bundle.text.0 = text.to_string();
        self
    }

    /// Set tooltip background style.
    fn tooltip_style(mut self, style: ContainerStyle) -> Self {
        self.set_tooltip().style = style;
        self
    }

    /// Set tooltip text style.
    fn tooltip_text_style(mut self, text_style: TextStyle) -> Self {
        self.set_tooltip().text_bundle.text_style = text_style;
        self
    }

    /// Set tooltip position.
    /// Available positions: `left`, `right`, `top`, `bottom`, `center`.
    /// Default is `center`.
    fn tooltip_position(mut self, pos: &str) -> Self {
        match pos {
            "left" => self.set_tooltip().position = TooltipPosition::Left,
            "right" => self.set_tooltip().position = TooltipPosition::Right,
            "top" => self.set_tooltip().position = TooltipPosition::Top,
            "bottom" => self.set_tooltip().position = TooltipPosition::Bottom,
            _ => self.set_tooltip().position = TooltipPosition::Center,
        }
        self
    }
}

pub trait SetIdAndClass: Sized {
    fn id_and_class(&mut self) -> &mut IdAndClass;

    fn id(mut self, id: &str) -> Self {
        self.id_and_class().id.0 = id.to_string();
        self
    }

    fn class(mut self, class: &str) -> Self {
        self.id_and_class().class.0 = class.to_string();
        self
    }
}

pub trait SetContainerStyle: Sized {
    fn container_style(&mut self) -> &mut ContainerStyle;

    fn node(mut self, node: Node) -> Self {
        self.container_style().node = node;
        self
    }

    fn width(mut self, value: Val) -> Self {
        self.container_style().node.width = value;
        self
    }

    fn height(mut self, value: Val) -> Self {
        self.container_style().node.height = value;
        self
    }

    fn align_items(mut self, ai: AlignItems) -> Self {
        self.container_style().node.align_items = ai;
        self
    }

    fn align_content(mut self, ac: AlignContent) -> Self {
        self.container_style().node.align_content = ac;
        self
    }

    fn justify_content(mut self, jc: JustifyContent) -> Self {
        self.container_style().node.justify_content = jc;
        self
    }

    fn margin(mut self, value: Val) -> Self {
        self.container_style().node.margin = UiRect::all(value);
        self
    }

    fn margin_x(mut self, value: Val) -> Self {
        self.container_style().node.margin.left = value;
        self.container_style().node.margin.right = value;
        self
    }

    fn margin_y(mut self, value: Val) -> Self {
        self.container_style().node.margin.top = value;
        self.container_style().node.margin.bottom = value;
        self
    }

    fn margin_top(mut self, value: Val) -> Self {
        self.container_style().node.margin.top = value;
        self
    }

    fn margin_right(mut self, value: Val) -> Self {
        self.container_style().node.margin.right = value;
        self
    }

    fn margin_bottom(mut self, value: Val) -> Self {
        self.container_style().node.margin.bottom = value;
        self
    }

    fn margin_left(mut self, value: Val) -> Self {
        self.container_style().node.margin.left = value;
        self
    }

    fn padding(mut self, value: Val) -> Self {
        self.container_style().node.padding = UiRect::all(value);
        self
    }

    fn padding_x(mut self, value: Val) -> Self {
        self.container_style().node.padding.left = value;
        self.container_style().node.padding.right = value;
        self
    }

    fn padding_y(mut self, value: Val) -> Self {
        self.container_style().node.padding.top = value;
        self.container_style().node.padding.bottom = value;
        self
    }

    fn padding_top(mut self, value: Val) -> Self {
        self.container_style().node.padding.top = value;
        self
    }

    fn padding_bottom(mut self, value: Val) -> Self {
        self.container_style().node.padding.bottom = value;
        self
    }

    fn padding_left(mut self, value: Val) -> Self {
        self.container_style().node.padding.left = value;
        self
    }

    fn padding_right(mut self, value: Val) -> Self {
        self.container_style().node.padding.right = value;
        self
    }

    fn border(mut self, value: Val) -> Self {
        self.container_style().node.border = UiRect::all(value);
        self
    }

    fn border_top(mut self, value: Val) -> Self {
        self.container_style().node.border.top = value;
        self
    }

    fn border_bottom(mut self, value: Val) -> Self {
        self.container_style().node.border.bottom = value;
        self
    }

    fn border_left(mut self, value: Val) -> Self {
        self.container_style().node.border.left = value;
        self
    }

    fn border_right(mut self, value: Val) -> Self {
        self.container_style().node.border.right = value;
        self
    }

    fn background_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().background_color.0 = color.into_color();
        self
    }

    fn border_radius(mut self, radius: BorderRadius) -> Self{
        self.container_style().node.border_radius = radius;
        self
    }

    fn border_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().border_color = BorderColor::all(color.into_color());
        self
    }

    fn border_top_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().border_color.top = color.into_color();
        self
    }

    fn border_bottom_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().border_color.bottom = color.into_color();
        self
    }

    fn border_left_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().border_color.left = color.into_color();
        self
    }

    fn border_right_color(mut self, color: impl IntoColor) -> Self {
        self.container_style().border_color.right = color.into_color();
        self
    }

    fn shadow(mut self, shadow: ShadowStyle) -> Self {
        self.container_style().shadow = BoxShadow(vec![shadow]);
        self
    }

    fn no_shadow(mut self) -> Self {
        self.container_style().shadow = BoxShadow::default();
        self
    }

    fn style(mut self, style: ContainerStyle) -> Self {
        *self.container_style() = style;
        self
    }
}

/// Component used to store id for a widget.
#[derive(Component, Debug, Default, PartialEq, Eq, Clone)]
pub struct Id(pub String);

/// Component used to store class for a widget.
#[derive(Component, Debug, Default, PartialEq, Eq, Clone)]
pub struct Class(pub String);

impl Class {
    /// Adds a class (or multiple space-separated classes) to the widget.
    pub fn add_class(&mut self, class: &str) {
        let mut classes: Vec<_> = self.0.split_whitespace().collect();

        for new_class in class.split_whitespace() {
            if !classes.contains(&new_class) {
                classes.push(new_class);
            }
        }

        self.0 = classes.join(" ");
    }

    /// Removes a specific class (or multiple) from the widget.
    pub fn remove_class(&mut self, class: &str) {
        let to_remove: Vec<_> = class.split_whitespace().collect();

        let classes: Vec<_> = self.0
            .split_whitespace()
            .filter(|c| !to_remove.contains(c))
            .collect();

        self.0 = classes.join(" ");
    }

    /// Returns an iterator over the individual class names.
    ///
    /// Useful for checking specific styles or logic conditions.
    pub fn class_list(&self) -> Vec<String> {
        self.0
            .split_whitespace()
            .map(|s| s.to_string())
            .collect()
    }

    /// Checks if the widget contains a specific class.
    pub fn has_class(&self, class: &str) -> bool {
        self.0.split_whitespace().any(|c| c == class)
    }
}

#[derive(Bundle, Clone, Default, Debug)]
pub struct IdAndClass {
    pub id: Id,
    pub class: Class
}

/// System param for based styling for all widgets.
#[derive(SystemParam)]
pub struct StyleQuery<'w, 's, F: QueryFilter + 'static = ()> {
    pub query: Query<'w, 's, (
        &'static mut Node,
        &'static mut BackgroundColor,
        &'static mut BorderColor,
        &'static mut BoxShadow,
        &'static mut ZIndex,
    ), F>,
}

/// A struct used to mutate styling components for text.
/// This struct is only used within other widgets.
pub struct ChildText<'a> {
    pub value: &'a mut Text,
    pub font: &'a mut TextFont,
    pub layout: &'a mut TextLayout,
    pub color: &'a mut TextColor
}

/// A struct used to mutate styling components for widgets.
pub struct WidgetStyle<'a> {
    pub node: &'a mut Node,
    pub background_color: &'a mut BackgroundColor,
    pub border_color: &'a mut BorderColor,
    pub z_index: &'a mut ZIndex,
    pub shadow: &'a mut BoxShadow
}

/// Trait used for widgets system param.
/// 
/// # Navigation Patterns
/// 
/// When using widget queries in event handlers that may navigate, use the `navigate!` macro
/// to automatically exit the function after navigation:
/// 
/// ```rust
/// fn on_button_click(
///     _click: On<Clicked>,
///     mut text_q: TextQuery,
///     mut router: ResMut<Router>
/// ) {
///     if let Some(mut text) = text_q.find_by_id("status") {
///         text.set_text("Navigating...");
///     }
///     
///     navigate!(router, "next-page", ()); // Function exits here automatically
///     // This code will never execute
/// }
/// ```
/// 
/// This prevents common issues where loops or code continue executing after navigation:
/// 
/// ```rust
/// fn mark_items_complete(
///     _click: On<Clicked>,
///     mut btn_q: ButtonQuery,
///     mut router: ResMut<Router>
/// ) {
///     for entity in btn_q.find_by_class("item-btn") {
///         if let Some(mut btn) = btn_q.find_by_entity(entity) {
///             if btn.text.value.0 == "target_item" {
///                 btn.class.add_class("completed");
///                 navigate!(router, "home", ()); // Exits function and loop
///             }
///         }
///     }
/// }
/// ```
pub trait WidgetQuery<'w, 's> {
    /// The specific view struct this query returns (Ex, ButtonWidget, TextWidget).
    type WidgetView<'a> where Self: 'a;

    /// Method to find widget by ID.
    fn find_by_id<'a>(&'a mut self, target_id: &str) -> Option<Self::WidgetView<'a>>;

    /// Method to find widget by Entity.
    fn find_by_entity<'a>(&'a mut self, entity: Entity) -> Option<Self::WidgetView<'a>>;

    /// Method to find entities that match with provided classes.
    fn find_by_class(&self, target_class: &str) -> Vec<Entity>;

    /// Get related components of an entity.
    fn get_components<'a>(&'a mut self, entity: Entity) -> Option<Self::WidgetView<'a>>;
}

/// Trait used for widgets that accept child/children.
pub trait WidgetChildren {
    /// Add a single child to a widget.
    fn add_child(&mut self, child_bundle: impl Bundle);

    /// Add multiple children to a widget.
    fn add_children(&mut self, bundles: impl IntoIterator<Item = impl Bundle>);

    /// Insert children at given index.
    fn insert_at(
        &mut self,
        index: usize,
        bundles: impl IntoIterator<Item = impl Bundle>
    );

    /// Insert children at the beginning.
    fn insert_first(&mut self, bundles: impl IntoIterator<Item = impl Bundle>);

    /// Insert children at the end.
    fn insert_last(&mut self, bundles: impl IntoIterator<Item = impl Bundle>);

    /// Remove a child at given index. Does nothing if index out of bound.
    /// This will remove the child from hierachy and UI world.
    fn remove_at(&mut self, index: usize);

    /// Remove the first child.
    /// This will remove the child from hierachy and UI world.
    fn remove_first(&mut self);

    /// Remove the last child.
    /// This will remove the child from hierachy and UI world.
    fn remove_last(&mut self);
}

#[derive(Component)]
pub(crate) struct MakaraWidget;

/// Component used to store focus state of a widget.
#[derive(Component)]
pub struct WidgetFocus(pub bool);

/// Style for all container box. Example, button, row, column ..
#[derive(Bundle, Clone)]
pub struct ContainerStyle {
    pub node: Node,
    pub background_color: BackgroundColor,
    pub border_color: BorderColor,
    pub shadow: BoxShadow,
    pub z_index: ZIndex,
}

impl Default for ContainerStyle {
    fn default() -> Self {
        Self {
            node: Node::default(),
            background_color: BackgroundColor::default(),
            border_color: BorderColor::default(),
            z_index: ZIndex::default(),
            shadow: BoxShadow::new(
                Color::BLACK.with_alpha(0.8),
                px(0.0),
                px(1.0),
                px(1.0),
                px(2.0),
            ),
        }
    }
}

/// Type of theme for makara application.
#[derive(Default, PartialEq, Eq)]
pub enum Theme {
    #[default]
    Light,
    Dark
}

/// Resource used to track current theme.
#[derive(Resource, Default)]
pub struct MakaraTheme {
    pub theme: Theme
}

impl MakaraTheme {
    /// Change current theme to the provided one.
    pub fn change_theme(&mut self, theme: Theme) {
        self.theme = theme;
    }
}

/// Resource used to store related data for cosmic-text.
#[derive(Resource)]
pub struct MakaraTextEditContext {
    pub font_system: FontSystem,
    pub swash_cache: SwashCache,
    pub attrs: Attrs<'static>
}

impl Default for MakaraTextEditContext {
    fn default() -> Self {
        Self {
            font_system: FontSystem::new(),
            swash_cache: SwashCache::new(),
            attrs: Attrs::new()
        }
    }
}

// mouse out observer for widget
pub(crate) fn on_mouse_out(
    mut out: On<Pointer<Out>>,
    mut commands: Commands,
    mut tooltips: Query<
        (&mut Node, &ComputedNode, &TooltipPosition, &UseTooltip),
        With<MakaraTooltip>
    >,
    widgets: Query<&Children, With<MakaraWidget>>,
    window: Single<Entity, With<Window>>,
) {
    if let Ok(children) = widgets.get(out.entity) {
        show_or_hide_tooltip(false, &mut tooltips, None, None, children);
    }

    commands.entity(*window).insert(CursorIcon::System(SystemCursorIcon::Default));
    commands.trigger(MouseOut { entity: out.entity });
    out.propagate(false);
}