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
use crate::{ draw_command::Aligment, types::{Color, Rect, Vector2}, Id, Layout, Ui, }; pub struct Tabbar<'a> { id: Id, size: Vector2, selected_tab: Option<usize>, tabs: &'a [&'a str], } impl Tabbar<'_> { pub fn new<'a>(id: Id, size: Vector2, tabs: &'a [&'a str]) -> Tabbar<'a> { Tabbar { id, size, tabs, selected_tab: None, } } pub fn selected_tab(self, selected_tab: Option<usize>) -> Self { Tabbar { selected_tab, ..self } } pub fn ui(self, ui: &mut Ui) -> u32 { let context = ui.get_active_window_context(); let pos = context.window.cursor.fit(self.size, Layout::Vertical); let width = self.size.x as f32 / self.tabs.len() as f32; let selected = { let selected_mut = context.storage_u32.entry(self.id).or_insert(0); if let Some(n) = self.selected_tab { *selected_mut = n as u32; }; *selected_mut }; for (n, label) in self.tabs.iter().enumerate() { let rect = Rect::new( pos.x + width * n as f32 + 1., pos.y, width - 2., self.size.y, ); let hovered = rect.contains(context.input.mouse_position); let selected = n as u32 == selected; if context.focused && hovered && context.input.click_up { *context.storage_u32.entry(self.id).or_insert(0) = n as u32; } context.window.draw_commands.draw_rect( rect, None, context.global_style.tabbar_background( context.focused, selected, hovered, hovered && context.input.is_mouse_down, ), ); let text_width = context.window.draw_commands.label_size(label, None).x; context.window.draw_commands.draw_label( label, pos + Vector2::new( width * n as f32 + (width - text_width) / 2., context.global_style.margin_button + 2., ), ( if selected { Color::new(1., 1., 1., 1.) } else { context.global_style.text(context.focused) }, Aligment::Center, ), ); } context.storage_u32[&self.id] } } impl Ui { pub fn tabbar<'a>( &mut self, id: Id, size: Vector2, tabs: &'a [&'a str], ) -> u32 { Tabbar::new(id, size, tabs).ui(self) } }