-
-
Notifications
You must be signed in to change notification settings - Fork 455
Force reload theme resources when changing fonts etc in settings #1283
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
Conversation
#637) This will mean that changing some settings will be a bit slower than before, especially when enabling blur which will reload resources twice but this is negligible and only hits in settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there, thank you for taking the time to look into this issue and making a fix. The current code is not written with enough flexibility to allow directly calling the required changes to fonts without going through the whole ChangeTheme call, which is confusing and probably why you needed to move UpdateResourceDictionary method out of the if condition.
I think a better design is to move the query font update code in this UpdateResourceDictionary method into its own method so you can call it directly from the view model:
Flow.Launcher/Flow.Launcher.Core/Resource/Theme.cs
Lines 149 to 160 in 8942e55
if (dict["QueryBoxStyle"] is Style queryBoxStyle && dict["QuerySuggestionBoxStyle"] is Style querySuggestionBoxStyle) { var fontFamily = new FontFamily(Settings.QueryBoxFont); var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.QueryBoxFontStyle); var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.QueryBoxFontWeight); var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.QueryBoxFontStretch); queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, fontFamily)); queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, fontStyle)); queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, fontWeight)); queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, fontStretch)); Flow.Launcher/Flow.Launcher.Core/Resource/Theme.cs
Lines 168 to 172 in 8942e55
// Query suggestion box's font style is aligned with query box querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, fontFamily)); querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, fontStyle)); querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, fontWeight)); querySuggestionBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, fontStretch));
Also another method for updating the result fonts:
Flow.Launcher/Flow.Launcher.Core/Resource/Theme.cs
Lines 175 to 191 in 8942e55
| if (dict["ItemTitleStyle"] is Style resultItemStyle && | |
| dict["ItemSubTitleStyle"] is Style resultSubItemStyle && | |
| dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle && | |
| dict["ItemTitleSelectedStyle"] is Style resultItemSelectedStyle && | |
| dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle && | |
| dict["ItemHotkeySelectedStyle"] is Style resultHotkeyItemSelectedStyle) | |
| { | |
| Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(Settings.ResultFont)); | |
| Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.ResultFontStyle)); | |
| Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.ResultFontWeight)); | |
| Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.ResultFontStretch)); | |
| Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch }; | |
| Array.ForEach( | |
| new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle, resultHotkeyItemStyle, resultHotkeyItemSelectedStyle }, o | |
| => Array.ForEach(setters, p => o.Setters.Add(p))); | |
| } |
So then you can call them directly from the view model here:
public FontFamily SelectedQueryBoxFont public FamilyTypeface SelectedQueryBoxFontFaces public FontFamily SelectedResultFont public FamilyTypeface SelectedResultFontFaces
Let me know if you can make these changes, happy to help/make them also :)
|
Thanks for the detailed response! I looked around for ways to do what you're suggesting, and avoided it because of how invasive it was :) I'll see if I can spend some time this weekend refactoring some of the code as you suggest and then resubmit! |
|
Fantastic. You can just push the new changes to this PR :) |
|
I spent a bit of time looking at this today and it's not clear that a refactor actually achieves much. Additionally, to change this you either need to edit the live resource dictionaries or reapply them anyway using Reloading the full monte on font change is relatively cheap, since it only happens in Settings, and is safe as it just reloads everything. A different option could be to call the resource dictionary updates directly (i.e. |
|
why is this one closed? I think you are right that it is quite hard to refactor? |
|
I was futzing with branches and github decided to be helpful and close the PR. I still want to fix this one... If there is consensus on the original fix (or a different one) then I will bring it back as a new PR. |
Sure, the original code sounds good for me though (as a fix). I think what @jjw24 would like to do is to rewrite the whole theme loading system, which may take a bit more time. |
|
Yeah, I will do a small rewrite and push up soon. |
Let me know if you want me to re-PR the original fix, or a different fix if the overall refactor is going to take a while 😀 |
|
nah should be fine, let's keep in this PR, not a big refactor and you were right, it's not going to bring much efficiency but at least the code will be more flexible and readable. Will find some time today or tomorrow. |
|
I don't think I will have time to do a proper rewrite, will merge in and tackle it later when needed. |
Force reload theme resources when changing fonts etc in settings
* Merge pull request #1061 from Flow-Launcher/remove_winget_ci * Merge pull request #991 from Flow-Launcher/context_menu_plugin_site * Merge pull request #1080 from gissehel/caret-position-fix * Caret position fix : Include PR #1074 * Merge pull request #1283 from nachmore/dev * Merge pull request #1296 from nachmore/bug_1284 * Merge pull request #1294 from Flow-Launcher/pluginInfoMultipleActionKeyword * Merge pull request #1299 from nachmore/bug_1269 * Plugin Exception Draft (#1147) * Merge pull request #1355 from onesounds/LimitWidth * Merge pull request #1088 from Flow-Launcher/add_spanish_latin_america * Merge pull request #1387 from Flow-Launcher/fix_exception_duplicate_url_opening * Merge pull request #1390 from Flow-Launcher/issue_1371 * Merge pull request #1391 from Flow-Launcher/issue_1366
Fixes #625 & #637
This will mean that changing some settings will be a bit slower than before, especially when enabling blur which will reload resources twice but this is negligible and only hits in settings.