theultraman20

learn_gpui.rs

Oct 7th, 2025
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.25 KB | None | 0 0
  1. use std::marker::PhantomData;
  2.  
  3. use gpui::{
  4.     App, Application, Context, Menu, MenuItem, Window,
  5.     WindowOptions, actions, div, prelude::*, rgb, Global
  6. };
  7.  
  8.  
  9. pub struct Theme {
  10.     primary: u32,
  11.     secondary: u32,
  12. }
  13.  
  14. impl From<(u32, u32)> for Theme {
  15.     fn from(tup: (u32, u32)) -> Theme {
  16.         Theme { primary: tup.0, secondary: tup.1 }
  17.     }
  18. }
  19.  
  20. pub const SPOTIFY_THEME: Theme = Theme {
  21.     primary: 0x1cd061,
  22.     secondary: 0x121212,
  23. };
  24.  
  25. pub const FIREFOX_THEME: Theme = Theme {
  26.     primary: 0xff5933,
  27.     secondary: 0x421c5f,
  28. };
  29.  
  30. pub const BLUEY_THEME: Theme = Theme {
  31.     primary: 0xf2d787,
  32.     secondary: 0x9acbec,
  33. };
  34.  
  35. pub const SPICY_THEME: Theme = Theme {
  36.     primary: 0x1b1b1b,
  37.     secondary: 0xffa31a,
  38. };
  39.  
  40. pub struct SetMenus;
  41.  
  42. pub struct ActiveTheme {
  43.     active_theme: Theme,
  44. }
  45.  
  46. impl Global for ActiveTheme {}
  47.  
  48. //what does this macro do exactly
  49. actions!(set_menus, [Quit]);
  50. actions!(set_menus, [SpotifyTheme]);
  51. actions!(set_menus, [FirefoxTheme]);
  52. actions!(set_menus, [BlueyTheme]);
  53. actions!(set_menus, [SpicyTheme]);
  54.  
  55.  
  56. impl Render for SetMenus {
  57.     fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
  58.         div()
  59.             .flex()
  60.             .bg(rgb(cx.global::<ActiveTheme>().active_theme.primary))
  61.             .size_full()
  62.             .justify_center()
  63.             .items_center()
  64.             .text_xl()
  65.             .text_color(rgb(cx.global::<ActiveTheme>().active_theme.secondary))
  66.             .child("Set Menus Example")
  67.     }
  68. }
  69.  
  70. fn main() {
  71.     Application::new().run(|cx: &mut App| {
  72.  
  73.         cx.set_global::<ActiveTheme>(ActiveTheme { active_theme: SPOTIFY_THEME });
  74.  
  75.         //??? idk what this does exactly
  76.         cx.activate(true);
  77.  
  78.         cx.on_action(|_: &Quit, cx: &mut App| cx.quit());
  79.         cx.on_action(|_: &FirefoxTheme, cx: &mut App| {
  80.             cx.set_global::<ActiveTheme>(ActiveTheme { active_theme: FIREFOX_THEME });
  81.             cx.notify();
  82.         });
  83.         cx.on_action(|_: &BlueyTheme, cx: &mut App| {
  84.             cx.set_global::<ActiveTheme>(ActiveTheme { active_theme: BLUEY_THEME });
  85.             cx.notify();
  86.         });
  87.         cx.on_action(|_: &SpotifyTheme, cx: &mut App| {
  88.             cx.set_global::<ActiveTheme>(ActiveTheme { active_theme: SPOTIFY_THEME });
  89.             cx.notify();
  90.         });
  91.         cx.on_action(|_: &SpicyTheme, cx: &mut App| {
  92.             cx.set_global::<ActiveTheme>(ActiveTheme { active_theme: SPICY_THEME });
  93.             cx.notify();
  94.         });
  95.  
  96.         cx.set_menus(vec![
  97.             Menu {
  98.                 name: "main".into(),
  99.                 items: vec![
  100.                     MenuItem::action("Quit", Quit),
  101.             ]},
  102.             Menu {
  103.                 name: "themes!!!".into(),
  104.                 items: vec![
  105.                     MenuItem::action("Spotify theme", SpotifyTheme),
  106.                     MenuItem::action("Firefox theme", FirefoxTheme),
  107.                     MenuItem::action("Bluey theme", BlueyTheme),
  108.                     MenuItem::action("Spicy theme", SpicyTheme),
  109.                 ],
  110.             },
  111.         ]);
  112.  
  113.         let set_menus =  cx.new(|_| {SetMenus});
  114.         cx.open_window(WindowOptions::default(), |_, _cx| { set_menus }).unwrap();
  115.  
  116.     })
  117. }
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment