Skip to content

Commit 5542359

Browse files
authored
refactor(core): Context fields now private, Icon used on all platforms (#1774)
1 parent 1df16b7 commit 5542359

File tree

6 files changed

+124
-61
lines changed

6 files changed

+124
-61
lines changed

.changes/private-context.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": patch
4+
"tauri-runtime-wry": patch
5+
---
6+
7+
**Breaking:** `Context` fields are now private, and is expected to be created through `Context::new(...)`.
8+
All fields previously available through `Context` are now public methods.

core/tauri-codegen/src/context.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
8888
.join(system_tray_icon_path)
8989
.display()
9090
.to_string();
91-
quote!(Some(::std::path::PathBuf::from(#system_tray_icon_path)))
91+
quote!(Some(#root::Icon::File(::std::path::PathBuf::from(#system_tray_icon_path))))
9292
} else {
9393
let system_tray_icon_file_path = system_tray_icon_path.to_string_lossy().to_string();
9494
quote!(
9595
Some(
96-
#root::api::path::resolve_path(
97-
&#config, &#package_info,
98-
#system_tray_icon_file_path,
99-
Some(#root::api::path::BaseDirectory::Resource)
100-
).expect("failed to resolve resource dir")
96+
#root::Icon::File(
97+
#root::api::path::resolve_path(
98+
&#config, &#package_info,
99+
#system_tray_icon_file_path,
100+
Some(#root::api::path::BaseDirectory::Resource)
101+
).expect("failed to resolve resource dir")
102+
)
101103
)
102104
)
103105
}
@@ -113,17 +115,17 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
113115
.join(system_tray_icon_path)
114116
.display()
115117
.to_string();
116-
quote!(Some(include_bytes!(#system_tray_icon_path).to_vec()))
118+
quote!(Some(#root::Icon::Raw(include_bytes!(#system_tray_icon_path).to_vec())))
117119
} else {
118120
quote!(None)
119121
};
120122

121123
// double braces are purposeful to force the code into a block expression
122-
Ok(quote!(#root::Context {
123-
system_tray_icon: #system_tray_icon,
124-
config: #config,
125-
assets: ::std::sync::Arc::new(#assets),
126-
default_window_icon: #default_window_icon,
127-
package_info: #package_info,
128-
}))
124+
Ok(quote!(#root::Context::new(
125+
#config,
126+
::std::sync::Arc::new(#assets),
127+
#default_window_icon,
128+
#system_tray_icon,
129+
#package_info,
130+
)))
129131
}

core/tauri-runtime-wry/src/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -820,30 +820,34 @@ impl Runtime for Wry {
820820
Ok(DetachedWindow { label, dispatcher })
821821
}
822822

823-
#[cfg(all(feature = "system-tray", target_os = "linux"))]
823+
#[cfg(feature = "system-tray")]
824824
fn system_tray<I: MenuId>(
825825
&self,
826-
icon: std::path::PathBuf,
826+
icon: Icon,
827827
menu_items: Vec<SystemTrayMenuItem<I>>,
828828
) -> Result<()> {
829-
SystemTrayBuilder::new(
830-
icon,
831-
menu_items
832-
.into_iter()
833-
.map(|m| MenuItemWrapper::from(m).0)
834-
.collect(),
835-
)
836-
.build(&self.event_loop)
837-
.map_err(|e| Error::SystemTray(Box::new(e)))?;
838-
Ok(())
839-
}
829+
// todo: fix this interface in Tao to an enum similar to Icon
830+
831+
// we expect the code that passes the Icon enum to have already checked the platform.
832+
let icon = match icon {
833+
#[cfg(target_os = "linux")]
834+
Icon::File(path) => path,
835+
836+
#[cfg(not(target_os = "linux"))]
837+
Icon::Raw(bytes) => bytes,
838+
839+
#[cfg(target_os = "linux")]
840+
Icon::Raw(_) => {
841+
panic!("linux requires the system menu icon to be a file path, not bytes.")
842+
}
843+
844+
#[cfg(not(target_os = "linux"))]
845+
Icon::File(_) => {
846+
panic!("non-linux system menu icons must be bytes, not a file path",)
847+
}
848+
_ => unreachable!(),
849+
};
840850

841-
#[cfg(all(feature = "system-tray", not(target_os = "linux")))]
842-
fn system_tray<I: MenuId>(
843-
&self,
844-
icon: Vec<u8>,
845-
menu_items: Vec<SystemTrayMenuItem<I>>,
846-
) -> Result<()> {
847851
SystemTrayBuilder::new(
848852
icon,
849853
menu_items

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,11 @@ pub trait Runtime: Sized + 'static {
119119
) -> crate::Result<DetachedWindow<P>>;
120120

121121
/// Adds the icon to the system tray with the specified menu items.
122-
#[cfg(all(feature = "system-tray", target_os = "linux"))]
123-
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "system-tray", target_os = "linux"))))]
124-
fn system_tray<I: MenuId>(
125-
&self,
126-
icon: std::path::PathBuf,
127-
menu: Vec<menu::SystemTrayMenuItem<I>>,
128-
) -> crate::Result<()>;
129-
130-
/// Adds the icon to the system tray with the specified menu items.
131-
#[cfg(all(feature = "system-tray", not(target_os = "linux")))]
132-
#[cfg_attr(
133-
doc_cfg,
134-
doc(cfg(all(feature = "system-tray", not(target_os = "linux"))))
135-
)]
122+
#[cfg(feature = "system-tray")]
123+
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
136124
fn system_tray<I: MenuId>(
137125
&self,
138-
icon: Vec<u8>,
126+
icon: Icon,
139127
menu: Vec<menu::SystemTrayMenuItem<I>>,
140128
) -> crate::Result<()>;
141129

core/tauri/src/app.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::{collections::HashMap, sync::Arc};
2323
#[cfg(feature = "menu")]
2424
use crate::runtime::menu::Menu;
2525
#[cfg(feature = "system-tray")]
26-
use crate::runtime::menu::SystemTrayMenuItem;
26+
use crate::runtime::{menu::SystemTrayMenuItem, Icon};
2727

2828
#[cfg(feature = "updater")]
2929
use crate::updater;
@@ -474,7 +474,32 @@ where
474474
/// Runs the configured Tauri application.
475475
pub fn run(mut self, context: Context<A>) -> crate::Result<()> {
476476
#[cfg(feature = "system-tray")]
477-
let system_tray_icon = context.system_tray_icon.clone();
477+
let system_tray_icon = {
478+
let icon = context.system_tray_icon.clone();
479+
480+
// check the icon format if the system tray is supposed to be ran
481+
if !self.system_tray.is_empty() {
482+
use std::io::{Error, ErrorKind};
483+
#[cfg(target_os = "linux")]
484+
if let Some(Icon::Raw(_)) = icon {
485+
return Err(crate::Error::InvalidIcon(Box::new(Error::new(
486+
ErrorKind::InvalidInput,
487+
"system tray icons on linux must be a file path",
488+
))));
489+
}
490+
491+
#[cfg(not(target_os = "linux"))]
492+
if let Some(Icon::File(bytes)) = icon {
493+
return Err(crate::Error::InvalidIcon(Box::new(Error::new(
494+
ErrorKind::InvalidInput,
495+
"system tray icons on non-linux platforms must be the raw bytes",
496+
))));
497+
}
498+
}
499+
500+
icon
501+
};
502+
478503
let manager = WindowManager::with_handlers(
479504
context,
480505
self.plugins,

core/tauri/src/lib.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub use {
7272
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Pixel, Position, Size},
7373
WindowEvent,
7474
},
75-
MenuId, Params,
75+
Icon, MenuId, Params,
7676
},
7777
self::state::{State, StateManager},
7878
self::window::{Monitor, Window},
@@ -128,25 +128,61 @@ macro_rules! tauri_build_context {
128128
/// This is the output of the `tauri::generate_context!` macro, and is not considered part of the stable API.
129129
/// Unless you know what you are doing and are prepared for this type to have breaking changes, do not create it yourself.
130130
pub struct Context<A: Assets> {
131+
pub(crate) config: Config,
132+
pub(crate) assets: Arc<A>,
133+
pub(crate) default_window_icon: Option<Vec<u8>>,
134+
pub(crate) system_tray_icon: Option<Icon>,
135+
pub(crate) package_info: crate::api::PackageInfo,
136+
}
137+
138+
impl<A: Assets> Context<A> {
131139
/// The config the application was prepared with.
132-
pub config: Config,
140+
#[inline(always)]
141+
pub fn config(&self) -> &Config {
142+
&self.config
143+
}
133144

134145
/// The assets to be served directly by Tauri.
135-
pub assets: Arc<A>,
146+
#[inline(always)]
147+
pub fn assets(&self) -> Arc<A> {
148+
self.assets.clone()
149+
}
136150

137151
/// The default window icon Tauri should use when creating windows.
138-
pub default_window_icon: Option<Vec<u8>>,
139-
140-
/// The icon to use use on the system tray UI.
141-
#[cfg(target_os = "linux")]
142-
pub system_tray_icon: Option<std::path::PathBuf>,
152+
#[inline(always)]
153+
pub fn default_window_icon(&self) -> Option<&[u8]> {
154+
self.default_window_icon.as_deref()
155+
}
143156

144157
/// The icon to use use on the system tray UI.
145-
#[cfg(not(target_os = "linux"))]
146-
pub system_tray_icon: Option<Vec<u8>>,
158+
#[inline(always)]
159+
pub fn system_tray_icon(&self) -> Option<&Icon> {
160+
self.system_tray_icon.as_ref()
161+
}
147162

148163
/// Package information.
149-
pub package_info: crate::api::PackageInfo,
164+
#[inline(always)]
165+
pub fn package_info(&self) -> &crate::api::PackageInfo {
166+
&self.package_info
167+
}
168+
169+
/// Create a new [`Context`] from the minimal required items.
170+
#[inline(always)]
171+
pub fn new(
172+
config: Config,
173+
assets: Arc<A>,
174+
default_window_icon: Option<Vec<u8>>,
175+
system_tray_icon: Option<Icon>,
176+
package_info: crate::api::PackageInfo,
177+
) -> Self {
178+
Self {
179+
config,
180+
assets,
181+
default_window_icon,
182+
system_tray_icon,
183+
package_info,
184+
}
185+
}
150186
}
151187

152188
// TODO: expand these docs

0 commit comments

Comments
 (0)