-
Notifications
You must be signed in to change notification settings - Fork 7
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
Update webview theme on editor theme change #85
Changes from all commits
675c62e
8c50b68
c5fd854
1c3a1c6
3c5cbae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Input; | ||
using Cody.Core.Logging; | ||
|
||
namespace Cody.UI.Controls | ||
{ | ||
|
@@ -16,6 +17,8 @@ public class WebviewController | |
|
||
private ICommand _sendMessageCommand; | ||
|
||
private ILog _logger; | ||
|
||
public WebviewController() | ||
{ | ||
} | ||
|
@@ -135,10 +138,27 @@ private async Task ApplyThemingScript() | |
public void SetThemeScript(string colorThemeScript) | ||
{ | ||
_colorThemeScript = colorThemeScript; | ||
// We might want to apply the theme immediately if the webview is already loaded. | ||
// if (_webview.CoreWebView2.IsDocumentOpen) { | ||
// _ = ApplyThemingScript(); | ||
// } | ||
} | ||
|
||
public async void OnThemeChanged(object sender, IColorThemeChangedEvent e) | ||
{ | ||
try | ||
{ | ||
string updatedScript = e.ThemingScript; | ||
if (updatedScript != _colorThemeScript) | ||
{ | ||
_logger.Debug("Applying VS theme change to WebView ..."); | ||
|
||
SetThemeScript(updatedScript); | ||
await ApplyThemingScript(); | ||
|
||
_logger.Debug("Theme change applied."); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error("Applying theme change to WebView failed.", ex); | ||
} | ||
} | ||
|
||
public void SetHtml(string html) | ||
|
@@ -189,9 +209,16 @@ public void SetHtml(string html) | |
"; | ||
|
||
private static string GetThemeScript(string colorTheme) => $@" | ||
document.documentElement.dataset.ide = 'VisualStudio'; | ||
|
||
{colorTheme} | ||
(function() {{ | ||
document.documentElement.dataset.ide = 'VisualStudio'; | ||
document.documentElement.style = ''; | ||
{colorTheme} | ||
}})(); | ||
"; | ||
|
||
public void SetLogger(ILog logger) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Controller can use logger now :) |
||
_logger = logger; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using Cody.Core.Infrastructure; | ||
using Cody.Core.Infrastructure; | ||
using Microsoft.VisualStudio.PlatformUI; | ||
using Microsoft.VisualStudio.Settings; | ||
using Microsoft.VisualStudio.Shell; | ||
|
@@ -10,19 +10,39 @@ | |
using System.Drawing; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Cody.Core.Logging; | ||
|
||
namespace Cody.VisualStudio.Services | ||
{ | ||
public class ThemeService : IThemeService | ||
{ | ||
private IServiceProvider serviceProvider; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ILog _logger; | ||
|
||
public ThemeService(IServiceProvider serviceProvider) | ||
public event EventHandler<IColorThemeChangedEvent> ThemeChanged; | ||
|
||
public ThemeService(IServiceProvider serviceProvider, ILog logger) | ||
{ | ||
this.serviceProvider = serviceProvider; | ||
_serviceProvider = serviceProvider; | ||
_logger = logger; | ||
|
||
VSColorTheme.ThemeChanged += HandleThemeChanges; | ||
} | ||
|
||
// TODO: Update the webviews when the theme changes. | ||
// VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged; | ||
|
||
private async void HandleThemeChanges(ThemeChangedEventArgs e) | ||
{ | ||
try | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added try/catch here :) In general every code that is handling event should be in the try/catch. Also Task.Delay(100).Wait() was blocking UI thread, so changed the method signature to allow awaiting it. |
||
await Task.Delay(1000); // Short delay to allow VS to update colors | ||
var latest = GetThemingScript(); | ||
ThemeChanged?.Invoke(this, new IColorThemeChangedEvent { ThemingScript = latest }); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error("Processing VS Theme change failed.", ex); | ||
} | ||
} | ||
|
||
public IReadOnlyDictionary<string, string> GetColors() | ||
|
@@ -51,7 +71,7 @@ public FontInformation GetUIFont() | |
|
||
private FontInformation GetFontInfo(Guid categoryGuid) | ||
{ | ||
var storage = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(SVsFontAndColorStorage)); | ||
var storage = (IVsFontAndColorStorage)_serviceProvider.GetService(typeof(SVsFontAndColorStorage)); | ||
storage.OpenCategory(categoryGuid, (uint)(__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_READONLY)); | ||
|
||
var logFont = new LOGFONTW[1]; | ||
|
@@ -70,7 +90,7 @@ public bool IsDarkTheme() | |
const string darkTheme = "{1ded0138-47ce-435e-84ef-9ec1f439b749}"; | ||
const string systemTheme = "{619dac1e-8220-4bd9-96fb-75ceb61a6107}"; | ||
|
||
var settingsManager = new ShellSettingsManager(serviceProvider); | ||
var settingsManager = new ShellSettingsManager(_serviceProvider); | ||
var store = settingsManager.GetReadOnlySettingsStore(SettingsScope.UserSettings); | ||
var themeId = store.GetString("Theme", "BackupThemeId"); | ||
|
||
|
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.
Additional try/catch because we're handling event.