fix.修复了快捷方式组件无法正常透明的问题。

This commit is contained in:
lincube
2026-04-13 16:26:23 +08:00
parent b933f3badf
commit ce5acf5bd7
2 changed files with 43 additions and 6 deletions

View File

@@ -8,9 +8,6 @@
x:Class="LanMountainDesktop.Views.Components.ShortcutWidget"> x:Class="LanMountainDesktop.Views.Components.ShortcutWidget">
<Border x:Name="RootBorder" <Border x:Name="RootBorder"
Background="{DynamicResource AdaptiveSurfaceRaisedBrush}"
BorderBrush="{DynamicResource AdaptiveButtonBorderBrush}"
BorderThickness="1"
CornerRadius="{DynamicResource DesignCornerRadiusComponent}" CornerRadius="{DynamicResource DesignCornerRadiusComponent}"
ClipToBounds="True"> ClipToBounds="True">
<Grid RowDefinitions="*,Auto" <Grid RowDefinitions="*,Auto"

View File

@@ -25,6 +25,7 @@ public partial class ShortcutWidget : UserControl, IDesktopComponentWidget, ICom
private bool _showBackground = true; private bool _showBackground = true;
private double _currentCellSize = 48; private double _currentCellSize = 48;
private bool _isDisposed; private bool _isDisposed;
private bool _chromeApplied;
private const double TapMovementThreshold = 10; private const double TapMovementThreshold = 10;
private const long TapTimeThresholdMs = 500; private const long TapTimeThresholdMs = 500;
@@ -40,9 +41,32 @@ public partial class ShortcutWidget : UserControl, IDesktopComponentWidget, ICom
{ {
InitializeComponent(); InitializeComponent();
DoubleTapped += OnDoubleTapped; DoubleTapped += OnDoubleTapped;
Loaded += OnLoaded;
UpdateDisplay(); UpdateDisplay();
} }
private void OnLoaded(object? sender, RoutedEventArgs e)
{
// ApplyChrome() may have been called before the control was attached to the visual tree,
// causing FindResource() to fail. Re-apply now that resources are available.
if (!_chromeApplied)
{
ApplyChrome();
}
// Subscribe to theme changes so the background follows theme updates.
var themeService = HostAppearanceThemeProvider.GetOrCreate();
themeService.Changed += OnAppearanceThemeChanged;
}
private void OnAppearanceThemeChanged(object? sender, AppearanceThemeSnapshot e)
{
if (_isDisposed || _showBackground)
{
ApplyChrome();
}
}
public void SetComponentPlacementContext(string componentId, string? placementId) public void SetComponentPlacementContext(string componentId, string? placementId)
{ {
_componentId = string.IsNullOrWhiteSpace(componentId) _componentId = string.IsNullOrWhiteSpace(componentId)
@@ -258,13 +282,25 @@ public partial class ShortcutWidget : UserControl, IDesktopComponentWidget, ICom
RootBorder.Background = Brushes.Transparent; RootBorder.Background = Brushes.Transparent;
RootBorder.BorderBrush = Brushes.Transparent; RootBorder.BorderBrush = Brushes.Transparent;
RootBorder.BorderThickness = new Thickness(0); RootBorder.BorderThickness = new Thickness(0);
_chromeApplied = true;
return; return;
} }
// 恢复默认的实心背景样式 // FindResource requires the control to be attached to the visual tree.
RootBorder.Background = this.FindResource("AdaptiveSurfaceRaisedBrush") as IBrush ?? Brushes.Transparent; // If it returns null, _chromeApplied stays false so OnLoaded will retry.
RootBorder.BorderBrush = this.FindResource("AdaptiveButtonBorderBrush") as IBrush ?? Brushes.Transparent; var background = this.FindResource("AdaptiveSurfaceRaisedBrush") as IBrush;
var borderBrush = this.FindResource("AdaptiveButtonBorderBrush") as IBrush;
if (background is null || borderBrush is null)
{
_chromeApplied = false;
return;
}
RootBorder.Background = background;
RootBorder.BorderBrush = borderBrush;
RootBorder.BorderThickness = new Thickness(1); RootBorder.BorderThickness = new Thickness(1);
_chromeApplied = true;
} }
protected override void OnPointerPressed(PointerPressedEventArgs e) protected override void OnPointerPressed(PointerPressedEventArgs e)
@@ -391,6 +427,10 @@ public partial class ShortcutWidget : UserControl, IDesktopComponentWidget, ICom
} }
_isDisposed = true; _isDisposed = true;
var themeService = HostAppearanceThemeProvider.GetOrCreate();
themeService.Changed -= OnAppearanceThemeChanged;
_gestureStates.Clear(); _gestureStates.Clear();
} }
} }