Skip to content
Snippets Groups Projects
Commit d185f2cd authored by MrGVSV's avatar MrGVSV
Browse files

Formatting

parent 6b95f265
No related branches found
No related tags found
No related merge requests found
use kayak_ui::{
core::{
render_command::RenderCommand,
rsx,
styles::{LayoutType, Style, StyleProp, Units},
Bound, EventType, OnEvent, rsx, use_state, widget,
use_state, widget, Bound, EventType, OnEvent,
},
widgets::{Background, Text},
};
......@@ -28,26 +29,24 @@ pub fn Tab(context: &mut KayakContext, content: String, selected: bool) {
_ => {}
};
let event_handler = OnEvent::new(move |_, event| {
match event.event_type {
EventType::Hover => {
if selected {
set_hover_state(TabHoverState::Active);
} else {
set_hover_state(TabHoverState::Inactive);
}
let event_handler = OnEvent::new(move |_, event| match event.event_type {
EventType::Hover => {
if selected {
set_hover_state(TabHoverState::Active);
} else {
set_hover_state(TabHoverState::Inactive);
}
EventType::MouseOut => {
set_hover_state(TabHoverState::None);
}
EventType::Focus => {
set_focus_state(true);
}
EventType::Blur => {
set_focus_state(false);
}
_ => {}
}
EventType::MouseOut => {
set_hover_state(TabHoverState::None);
}
EventType::Focus => {
set_focus_state(true);
}
EventType::Blur => {
set_focus_state(false);
}
_ => {}
});
let tab_color = match hover_state {
......@@ -108,4 +107,4 @@ pub fn Tab(context: &mut KayakContext, content: String, selected: bool) {
</Background>
</Background>
}
}
\ No newline at end of file
}
use kayak_ui::{
core::{
constructor, rsx,
styles::{LayoutType, Style, StyleProp, Units},
Bound, constructor, EventType, Handler, KeyCode, OnEvent,
rsx, VecTracker, widget,
widget, Bound, EventType, Handler, KeyCode, OnEvent, VecTracker,
},
widgets::Background,
};
......@@ -12,7 +12,12 @@ use crate::TabTheme;
/// A widget displaying a collection of tabs in a horizontal bar
#[widget]
pub fn TabBar(context: &mut KayakContext, tabs: Vec<String>, selected: usize, on_select_tab: Handler<usize>) {
pub fn TabBar(
context: &mut KayakContext,
tabs: Vec<String>,
selected: usize,
on_select_tab: Handler<usize>,
) {
let theme = context.create_consumer::<TabTheme>().unwrap_or_default();
let tabs = tabs.iter().enumerate().map(|(index, tab)| {
......@@ -50,4 +55,4 @@ pub fn TabBar(context: &mut KayakContext, tabs: Vec<String>, selected: usize, on
<VecTracker data={tabs} />
</Background>
}
}
\ No newline at end of file
}
use std::fmt::Debug;
use kayak_ui::{
core::{
render_command::RenderCommand,
styles::{Style, StyleProp},
Bound, Fragment, Handler, rsx, use_state, widget,
}
use kayak_ui::core::{
render_command::RenderCommand,
rsx,
styles::{Style, StyleProp},
use_state, widget, Bound, Fragment, Handler,
};
use std::fmt::Debug;
use crate::tab_bar::TabBar;
use crate::TabTheme;
use crate::tab_content::TabContent;
use crate::TabTheme;
#[derive(Debug, Default, Clone, PartialEq)]
pub struct TabData {
......@@ -27,12 +26,14 @@ pub fn TabBox(context: &mut KayakContext, tabs: Vec<TabData>, initial_tab: usize
let theme = context.create_consumer::<TabTheme>().unwrap_or_default();
let (selected, set_selected, ..) = use_state!(initial_tab);
let tab_names = tabs.iter().map(|tab| {
tab.name.clone()
}).collect::<Vec<String>>();
let tab_content = tabs.iter().map(|tab| {
tab.content.clone()
}).collect::<Vec<_>>();
let tab_names = tabs
.iter()
.map(|tab| tab.name.clone())
.collect::<Vec<String>>();
let tab_content = tabs
.iter()
.map(|tab| tab.content.clone())
.collect::<Vec<_>>();
let on_select_tab = Handler::<usize>::new(move |index| {
set_selected(index);
......@@ -50,4 +51,4 @@ pub fn TabBox(context: &mut KayakContext, tabs: Vec<TabData>, initial_tab: usize
<TabContent tabs={tab_content} selected={selected} />
</>
}
}
\ No newline at end of file
}
use std::ops::Index;
use kayak_ui::{
core::{
render_command::RenderCommand,
styles::{Style, StyleProp},
Bound, Fragment, rsx, VecTracker, widget,
}
use kayak_ui::core::{
render_command::RenderCommand,
rsx,
styles::{Style, StyleProp},
widget, Bound, Fragment, VecTracker,
};
use std::ops::Index;
use crate::TabTheme;
......@@ -32,4 +31,4 @@ pub fn TabContent(context: &mut KayakContext, tabs: Vec<Fragment>, selected: usi
<VecTracker data={vec![tab.clone()]} />
</>
}
}
\ No newline at end of file
}
......@@ -13,20 +13,21 @@ use bevy::{
use kayak_ui::{
bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle},
core::{
constructor, render, rsx,
styles::{Style, StyleProp, Units},
Children, Color, constructor, Index, render, rsx, widget
widget, Children, Color, Index,
},
widgets::{App, Text, Window},
};
use crate::theming::{ColorState, TabTheme, TabThemeProvider};
use tab_box::TabBox;
use tab_box::TabData;
use crate::theming::{ColorState, TabTheme, TabThemeProvider};
mod tab;
mod tab_bar;
mod tab_box;
mod tab_content;
mod tab;
mod theming;
#[widget]
......
use kayak_ui::{core::{Color, rsx, widget}};
use kayak_ui::core::{rsx, widget, Color};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TabTheme {
......@@ -9,7 +9,7 @@ pub struct TabTheme {
pub text: ColorState,
pub active_tab: ColorState,
pub inactive_tab: ColorState,
pub tab_height: f32
pub tab_height: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
......@@ -23,4 +23,4 @@ pub struct ColorState {
pub fn TabThemeProvider(initial_theme: TabTheme) {
context.create_provider(initial_theme);
rsx! { <>{children}</> }
}
\ No newline at end of file
}
......@@ -414,31 +414,29 @@ impl EventDispatcher {
/// Executes default actions for events
fn execute_default(&mut self, event: Event, context: &mut KayakContext) {
match event.event_type {
EventType::KeyDown(evt) => {
match evt.key() {
KeyCode::Tab => {
let current_focus = context.widget_manager.focus_tree.current();
EventType::KeyDown(evt) => match evt.key() {
KeyCode::Tab => {
let current_focus = context.widget_manager.focus_tree.current();
let index = if evt.is_shift_pressed() {
context.widget_manager.focus_tree.prev()
} else {
context.widget_manager.focus_tree.next()
};
if let Some(index) = index {
let mut events = vec![Event::new(index, EventType::Focus)];
if let Some(current_focus) = current_focus {
if current_focus != index {
events.push(Event::new(current_focus, EventType::Blur));
}
let index = if evt.is_shift_pressed() {
context.widget_manager.focus_tree.prev()
} else {
context.widget_manager.focus_tree.next()
};
if let Some(index) = index {
let mut events = vec![Event::new(index, EventType::Focus)];
if let Some(current_focus) = current_focus {
if current_focus != index {
events.push(Event::new(current_focus, EventType::Blur));
}
context.widget_manager.focus_tree.focus(index);
self.dispatch_events(events, context);
}
context.widget_manager.focus_tree.focus(index);
self.dispatch_events(events, context);
}
_ => {}
}
}
_ => {}
},
_ => {}
}
}
......
use std::collections::HashMap;
use crate::{Index, Tree};
use std::collections::HashMap;
#[derive(Debug, Default, PartialEq)]
pub struct FocusTree {
......@@ -157,7 +157,6 @@ impl FocusTree {
}
}
impl FocusTracker {
/// Set the focusability of a widget
///
......@@ -176,7 +175,12 @@ impl FocusTracker {
/// * `is_parent_defined`: Does this setting come from the parent or the widget itself?
///
/// returns: ()
pub fn set_focusability(&mut self, index: Index, focusable: Option<bool>, is_parent_defined: bool) {
pub fn set_focusability(
&mut self,
index: Index,
focusable: Option<bool>,
is_parent_defined: bool,
) {
let map = if is_parent_defined {
&mut self.parents
} else {
......@@ -208,7 +212,6 @@ impl FocusTracker {
}
}
#[cfg(test)]
mod tests {
use crate::focus_tree::FocusTree;
......@@ -299,4 +302,4 @@ mod tests {
// etc.
}
}
\ No newline at end of file
}
......@@ -30,8 +30,8 @@ pub use color::Color;
pub use context::*;
pub use cursor::PointerEvents;
pub use event::*;
pub use fragment::Fragment;
pub use focus_tree::FocusTree;
pub use fragment::Fragment;
pub use generational_arena::{Arena, Index};
pub use input_event::*;
pub use keyboard::{KeyboardEvent, KeyboardModifiers};
......
......@@ -66,7 +66,8 @@ impl Tree {
pub fn remove(&mut self, index: Index) -> Vec<Index> {
let parent = self.parents.remove(&index);
if let Some(parent) = parent {
let children = self.children
let children = self
.children
.remove(&index)
.unwrap_or_default()
.into_iter()
......@@ -141,7 +142,9 @@ impl Tree {
/// Returns true if the given node is in this tree
pub fn contains(&self, index: Index) -> bool {
Some(index) == self.root_node || self.parents.contains_key(&index) || self.children.contains_key(&index)
Some(index) == self.root_node
|| self.parents.contains_key(&index)
|| self.children.contains_key(&index)
}
/// Get the number of nodes in this tree
......@@ -212,9 +215,7 @@ impl Tree {
pub fn get_last_child(&self, index: Index) -> Option<Index> {
self.children.get(&index).map_or(None, |children| {
children
.last()
.map_or(None, |last_child| Some(*last_child))
children.last().map_or(None, |last_child| Some(*last_child))
})
}
......@@ -355,8 +356,8 @@ impl Tree {
let parent_b = parent_b.unwrap();
parent_a != parent_b
|| (parent_a == parent_b
&& *node != children_a.get(*id).unwrap().1
&& children_a.iter().any(|(_, node_b)| node == node_b))
&& *node != children_a.get(*id).unwrap().1
&& children_a.iter().any(|(_, node_b)| node == node_b))
} else {
false
};
......@@ -477,8 +478,8 @@ impl Tree {
let parent_b = parent_b.unwrap();
parent_a != parent_b
|| (parent_a == parent_b
&& *node != tree1.get(*id).unwrap().1
&& tree1.iter().any(|(_, node_b)| node == node_b))
&& *node != tree1.get(*id).unwrap().1
&& tree1.iter().any(|(_, node_b)| node == node_b))
} else {
false
};
......@@ -647,7 +648,7 @@ impl<'a> Iterator for DownwardIterator<'a> {
while current_parent.is_some() {
if let Some(current_parent) = current_parent {
if let Some(next_parent_sibling) =
self.tree.get_next_sibling(current_parent)
self.tree.get_next_sibling(current_parent)
{
self.current_node = Some(next_parent_sibling);
return Some(next_parent_sibling);
......@@ -845,22 +846,44 @@ mod tests {
expected.add(expected_grandchild_b, Some(expected_child_b));
tree.replace(grandchild_b, expected_grandchild_b);
assert!(tree.children.get(&child_b).unwrap().contains(&expected_grandchild_b));
assert!(tree
.children
.get(&child_b)
.unwrap()
.contains(&expected_grandchild_b));
assert!(!tree.children.get(&child_b).unwrap().contains(&grandchild_b));
tree.replace(grandchild_a, expected_grandchild_a);
assert!(tree.children.get(&child_a).unwrap().contains(&expected_grandchild_a));
assert!(tree
.children
.get(&child_a)
.unwrap()
.contains(&expected_grandchild_a));
assert!(!tree.children.get(&child_a).unwrap().contains(&grandchild_a));
tree.replace(child_a, expected_child_a);
assert!(tree.children.get(&root).unwrap().contains(&expected_child_a));
assert!(tree
.children
.get(&root)
.unwrap()
.contains(&expected_child_a));
assert!(!tree.children.get(&root).unwrap().contains(&child_a));
assert_eq!(expected_child_a, tree.get_parent(expected_grandchild_a).unwrap());
assert_eq!(
expected_child_a,
tree.get_parent(expected_grandchild_a).unwrap()
);
tree.replace(child_b, expected_child_b);
assert!(tree.children.get(&root).unwrap().contains(&expected_child_b));
assert!(tree
.children
.get(&root)
.unwrap()
.contains(&expected_child_b));
assert!(!tree.children.get(&root).unwrap().contains(&child_b));
assert_eq!(expected_child_b, tree.get_parent(expected_grandchild_b).unwrap());
assert_eq!(
expected_child_b,
tree.get_parent(expected_grandchild_b).unwrap()
);
tree.replace(root, expected_root);
assert_eq!(Some(expected_root), tree.root_node);
......@@ -1004,4 +1027,4 @@ mod tests {
tree.add(grandchild, Some(child));
assert_eq!(3, tree.len());
}
}
\ No newline at end of file
}
......@@ -5,14 +5,14 @@ use std::{
use crate::layout_cache::Rect;
use crate::{
focus_tree::FocusTracker,
focus_tree::FocusTree,
layout_cache::LayoutCache,
node::{Node, NodeBuilder},
render_command::RenderCommand,
render_primitive::RenderPrimitive,
styles::Style,
tree::Tree,
focus_tree::FocusTree,
focus_tree::FocusTracker,
Arena, Index, Widget,
};
// use as_any::Downcast;
......@@ -98,7 +98,6 @@ impl WidgetManager {
self.set_focusable(widget.focusable(), widget_id, true);
}
// TODO: Figure a good way of diffing props passed to children of a widget
// that wont naturally-rerender it's children because of a lack of changes
// to it's own props.
......@@ -419,6 +418,7 @@ impl WidgetManager {
}
pub fn set_focusable(&mut self, focusable: Option<bool>, index: Index, is_parent: bool) {
self.focus_tracker.set_focusability(index, focusable, is_parent);
self.focus_tracker
.set_focusability(index, focusable, is_parent);
}
}
......@@ -122,9 +122,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
},
),
(
vec![
"focusable : Option < bool >",
],
vec!["focusable : Option < bool >"],
quote! {
#[derivative(Default(value=#focusable_default))]
pub focusable: Option<bool>
......@@ -139,7 +137,8 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
.any(|name| block_name.to_string().contains(name))
}) {
input_block_names.push(token);
} else {}
} else {
}
}
let inputs_block = quote!(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment