Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThemePlugin not defined #13

Closed
MatthewScholefield opened this issue Jul 7, 2024 · 6 comments
Closed

ThemePlugin not defined #13

MatthewScholefield opened this issue Jul 7, 2024 · 6 comments

Comments

@MatthewScholefield
Copy link

MatthewScholefield commented Jul 7, 2024

The exact code on the README doesn't seem to work:

use tauri_plugin_theme::ThemePlugin;
error[E0432]: unresolved import `tauri_plugin_theme::ThemePlugin`
 --> src/main.rs:4:5
  |
4 | use tauri_plugin_theme::ThemePlugin;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `ThemePlugin` in the root

I had to change it to:

use tauri_plugin_theme;

Not sure if this is something that changed in the new version but afterwards everything works great!

@wyhaya
Copy link
Owner

wyhaya commented Jul 7, 2024

@MatthewScholefield This is just a documentation error, I forgot to change README.md.

If you are using Tauri v1

tauri_plugin_theme::ThemePlugin::init(ctx.config_mut());

If you are using Tauri v2

tauri_plugin_theme::init(ctx.config_mut());

@wyhaya
Copy link
Owner

wyhaya commented Aug 21, 2024

Note: A new version was released an hour ago. To avoid confusion, both v1 and v2 use the same API.

# For tauri@v1
cargo add tauri-plugin-theme@1

# For tauri@v2
cargo add tauri-plugin-theme@2
let mut ctx = tauri::generate_context!();
tauri::Builder::default()
    // ...
    .plugin(tauri_plugin_theme::init(ctx.config_mut()))
    // ...
    .run(ctx)
    // ...

@Yanyan99999
Copy link

when i use the theme on vue3, and code below:
async function getSystemTheme() {
const theme = await invoke('plugin:theme|theme')
console.log(theme)
} ; these code did not work, is there something wrong?

@wyhaya
Copy link
Owner

wyhaya commented Aug 21, 2024

when i use the theme on vue3, and code below: async function getSystemTheme() { const theme = await invoke('plugin:theme|theme') console.log(theme) } ; these code did not work, is there something wrong?

async function getSystemTheme() {
-   const theme = await invoke('plugin:theme|theme')
+   const theme = await invoke("plugin:theme|get_theme");
    console.log(theme)
} ; 

@Yanyan99999
Copy link

Yanyan99999 commented Aug 21, 2024

It works but all i get the theme is auto, even i changed it to dark. btw my system is redhat 9, and tauri v1, theme-plugin-theme@0.2.0

and i also tried to listen theme-changed, seems no response. code like below:

listen('tauri://theme-changed', (event) => {
const payload = event.payload as{theme: string}
console.log('theme change to:', payload.theme)
}); something wrong here?

@wyhaya
Copy link
Owner

wyhaya commented Aug 21, 2024

@Yanyan99999

It works but all i get the theme is auto, even i changed it to dark.

# 1.
invoke("plugin:theme|set_theme", {theme: 'dark'})
# 2.
$ cat ~/.config/{YOUR_APP_ID}/tauri-plugin-theme
# 3.
const theme = invoke("plugin:theme|get_theme")

# 4.
invoke("plugin:theme|set_theme", {theme: 'light'})
# 5.
$ cat ~/.config/{YOUR_APP_ID}/tauri-plugin-theme
# 6.
const theme = invoke("plugin:theme|get_theme")

The results of 2 and 3 should be the same, and the results of 5 and 6 should also be the same. Please check and see what you can find.

and i also tried to listen theme-changed, seems no response. code like below:

This plugin does not send events to Tauri. If you need to get the currently used theme, please use the following code:

const medie = window.matchMedia('(prefers-color-scheme: dark)')
medie.addEventListener('change', () => {
    const theme = medie.matches ? 'dark' : 'light'
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants