Skip to content

Commit 5307ea0

Browse files
authored
Merge pull request #1743 from unoplatform/dev/xygu/20261001/combobox-placeholder-resources
feat(material): expose ComboBox placeholder lightweight resources
2 parents 974e752 + a216278 commit 5307ea0

9 files changed

Lines changed: 356 additions & 5 deletions

File tree

‎doc/styles/ComboBox.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ IsDefaultStyle\*: Styles in this column will be set as the default implicit styl
110110
| `ComboBoxPlaceHolderForegroundFocusedPressed` | `SolidColorBrush` | `OnSurfaceVariantBrush` |
111111
| `ComboBoxPlaceHolderForegroundPointerOver` | `SolidColorBrush` | `OnSurfaceVariantBrush` |
112112
| `ComboBoxPlaceHolderForegroundPressed` | `SolidColorBrush` | `OnSurfaceVariantBrush` |
113+
| `ComboBoxPlaceholderTextStyle` | `Style` | `MaterialBodyLarge` |
113114
| `ComboBoxUpGlyphHeight` | `Double` | 5 |
114115
| `ComboBoxUpGlyphMargin` | `Thickness` | 7,0 |
115116
| `ComboBoxUpGlyphWidth` | `Double` | 10 |
@@ -120,7 +121,34 @@ IsDefaultStyle\*: Styles in this column will be set as the default implicit styl
120121
| `ComboBoxUpperPlaceHolderForegroundOpened` | `SolidColorBrush` | `PrimaryBrush` |
121122
| `ComboBoxUpperPlaceHolderForegroundPointerOver` | `SolidColorBrush` | `OnSurfaceVariantBrush` |
122123
| `ComboBoxUpperPlaceHolderForegroundPressed` | `SolidColorBrush` | `OnSurfaceVariantBrush` |
124+
| `ComboBoxUpperPlaceHolderMargin` | `Thickness` | 0 |
125+
| `ComboBoxUpperPlaceHolderScaleX` | `Double` | 0.7 |
126+
| `ComboBoxUpperPlaceHolderScaleY` | `Double` | 0.7 |
127+
| `ComboBoxUpperPlaceHolderTranslateY` | `Double` | -11 |
123128

124129
The Dark theme uses the same values as Light.
125130

126131
<!-- END GENERATED -->
132+
133+
### Placeholder
134+
135+
Once an item is selected, the Material ComboBox keeps its placeholder visible as a floating label: a scaled-down copy shown above the selected value. These keys apply to the Material style only:
136+
137+
- `ComboBoxUpperPlaceHolderScaleX`, `ComboBoxUpperPlaceHolderScaleY` and `ComboBoxUpperPlaceHolderTranslateY` size and position the floating label.
138+
- `ComboBoxUpperPlaceHolderMargin` offsets the floating label within the field. The label is centered in the same area as the selected value, so this moves the label only; the selected value keeps its fixed offset below it.
139+
- `ComboBoxPlaceholderTextStyle` is the text style of both the placeholder and the floating label. It must target `TextBlock`. The template sets `Foreground` on both and `Margin` on the floating label, so those setters in this style have no effect; use the `ComboBoxPlaceHolderForeground*`, `ComboBoxUpperPlaceHolderForeground*` and `ComboBoxUpperPlaceHolderMargin` keys instead.
140+
141+
> [!NOTE]
142+
> On Uno Platform targets, the `ScaleX`, `ScaleY` and `TranslateY` keys are looked up again from the application resources when the app theme changes at runtime. Overrides declared in `App.xaml` are kept, but overrides declared on a page or on the ComboBox itself revert to the defaults after a theme change. The `Margin` and text-style keys are not affected. See [unoplatform/uno#24958](https://github.com/unoplatform/uno/issues/24958).
143+
144+
```xml
145+
<ComboBox PlaceholderText="Label">
146+
<ComboBox.Resources>
147+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleX">0.6</x:Double>
148+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleY">0.6</x:Double>
149+
<x:Double x:Key="ComboBoxUpperPlaceHolderTranslateY">-13</x:Double>
150+
<Thickness x:Key="ComboBoxUpperPlaceHolderMargin">0,0,0,2</Thickness>
151+
<StaticResource x:Key="ComboBoxPlaceholderTextStyle" ResourceKey="BodyMedium" />
152+
</ComboBox.Resources>
153+
</ComboBox>
154+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Material ComboBox placeholder lightweight resources (issue #1739)
2+
3+
Implements https://github.com/unoplatform/Uno.Themes/issues/1739.
4+
5+
## Design
6+
7+
The Material v2 ComboBox floating label (`UpperPlaceholderElement`) hardcoded its `CompositeTransform`
8+
(`ScaleX`/`ScaleY` 0.7, `TranslateY` -11) and its text style (`MaterialBodyLarge`), so tuning it meant copying the
9+
template. Five additive lightweight keys now carry those values, with defaults equal to the former literals:
10+
11+
| Key | Type | Default | Used by |
12+
|---|---|---|---|
13+
| `ComboBoxUpperPlaceHolderScaleX` | `Double` | 0.7 | `UpperPlaceholderElement` transform |
14+
| `ComboBoxUpperPlaceHolderScaleY` | `Double` | 0.7 | `UpperPlaceholderElement` transform |
15+
| `ComboBoxUpperPlaceHolderTranslateY` | `Double` | -11 | `UpperPlaceholderElement` transform |
16+
| `ComboBoxUpperPlaceHolderMargin` | `Thickness` | 0 | `UpperPlaceholderElement` |
17+
| `ComboBoxPlaceholderTextStyle` | `Style` | `MaterialBodyLarge` | `PlaceholderElement` and `UpperPlaceholderElement` |
18+
19+
- Declared once at the root of Material v2 `SemanticStyles.xaml` (theme-invariant values, like `SmallThumbOffSize`), so
20+
they are `SemanticResources` and need no per-theme copies. The style key is a `StaticResource` alias, like the style
21+
aliases at the top of that file.
22+
- The template reads them through `{ThemeResource}` so a local override (control/page resources) wins.
23+
- The transform/margin keys keep the existing `PlaceHolder` casing of `ComboBoxUpperPlaceHolderForeground*`; the style
24+
key name `ComboBoxPlaceholderTextStyle` was chosen by the requester and covers both placeholders.
25+
- Material v2 only: Simple's ComboBox has no placeholder, Cupertino has no floating label and no semantic layer,
26+
Fluent templates are WinUI's and not shipped here, Material v1 drives its label through converters.
27+
- Not covered: the selected value's 5px offset (`NullToContentTranslateYConverter`) stays a converter literal (see Follow-ups).
28+
29+
## Checklist
30+
31+
- [x] Keys in `Styles/Controls/v2/SemanticStyles.xaml`
32+
- [x] `ComboBox.xaml` template consumes them
33+
- [x] C# Markup accessors (`Theme.ComboBox.Resources.Default.UpperPlaceHolder.*`, `PlaceholderTextStyle`)
34+
- [x] `doc/styles/ComboBox.md`
35+
- [x] Sample on `ComboBoxSamplePage` (M3)
36+
- [x] Runtime tests `Given_ComboBox` (Material sample app; the Simple host does not reference Uno.Material) + semantic
37+
membership in Material `Given_SemanticResources`
38+
- [x] Red run on the pre-change template, green after
39+
- [x] Full Material suite green; formatting gates clean
40+
41+
## Known limitation (Uno)
42+
43+
On a runtime theme change, Uno re-resolves the `{ThemeResource}` on the `CompositeTransform` (a non-FrameworkElement)
44+
from `Application.Resources` only, skipping the element's resource chain. Per-control/page overrides of the three
45+
transform keys therefore revert to the defaults after a theme switch; App.xaml overrides survive. Margin and the text
46+
style (FrameworkElement properties) keep local overrides in every scope. A `XamlReader.Load` template does not
47+
reproduce it, only the compiled library template. Native WinUI not verified. Documented in `doc/styles/ComboBox.md`;
48+
the tests assert what holds and name the gap. Upstream: https://github.com/unoplatform/uno/issues/24958.
49+
50+
## Follow-ups
51+
52+
- Uno: ThemeResource re-resolution on non-FrameworkElement objects in compiled templates (unoplatform/uno#24958).
53+
Once fixed, extend `When_ThemeChangesAfterLoad_Then_LocalMarginAndStyleAreKept` to the transform keys and drop the doc note.
54+
- The selected value's 5px offset (`NullToContentTranslateYConverter`) is still fixed, so a moved/rescaled label cannot
55+
take the value with it. Likely the next request.
56+
57+
## Review
58+
59+
- Red: `When_LightweightResourcesOverridden` failed 4/4 rows on the pre-change template; default test passed.
60+
- Green (desktop Skia, Debug): `Given_ComboBox` + `Given_SemanticResources` 22/22; full Material suite 95/95.
61+
- XamlStyler, `dotnet format whitespace`, cspell and markdownlint clean on the changed files.
62+
- Seven-agent review panel: no blockers; fixes applied (theme-switch coverage, all 5 keys in the semantic check, sample
63+
uses the style override, doc wording for Margin / style TargetType / ignored setters, this section).

‎specs/lessons.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,10 @@ next file to need an override didn't get it.
470470
- A pixel-identical sample at the default does not prove a template change is safe: the sample rarely exercises the case the change affects (long content, an override, another host size). Token remaps are provable by value; template changes are not.
471471
- List what token-only fixes leave broken in the issue as follow-ups instead of widening the change.
472472

473+
## `{ThemeResource}` on a non-FrameworkElement in a compiled template loses local overrides on theme change (#1739)
474+
475+
**Context:** #1739 made the Material ComboBox floating-label `CompositeTransform` (`ScaleX`/`ScaleY`/`TranslateY`) read lightweight keys through `{ThemeResource}`. Tests that set overrides before load passed. A test that switched `RequestedTheme` after load showed that Uno re-resolves those values from `Application.Resources` only, skipping the control and page resources, so local overrides reverted to the defaults. `Margin`, `Style` and `Foreground` on the same `TextBlock` kept their local overrides. A `XamlReader.Load` template did not reproduce it; only the compiled library template did. Upstream: unoplatform/uno#24958.
476+
477+
**How to apply:**
478+
- A lightweight key consumed by a non-FrameworkElement (transforms, brushes, gradient stops) inside a template needs a test that flips `RequestedTheme` after load with the override at the scope the doc promises. A pre-load assertion only proves the initial resolution.
479+
- Do not trust a `XamlReader` repro to clear a template-resolution bug: compiled library XAML takes a different path.

‎src/library/Uno.Material/Styles/Controls/v2/ComboBox.xaml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,25 @@
401401
IsHitTestVisible="False"
402402
MaxLines="1"
403403
RenderTransformOrigin="0,0.5"
404-
Style="{StaticResource MaterialBodyLarge}"
404+
Style="{ThemeResource ComboBoxPlaceholderTextStyle}"
405405
Text="{TemplateBinding PlaceholderText}" />
406406

407407
<TextBlock x:Name="UpperPlaceholderElement"
408408
Grid.Column="1"
409+
Margin="{ThemeResource ComboBoxUpperPlaceHolderMargin}"
409410
VerticalAlignment="Center"
410411
Visibility="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource NullToCollapsed}, TargetNullValue=Collapsed, FallbackValue=Collapsed}"
411412
Foreground="{ThemeResource ComboBoxUpperPlaceHolderForeground}"
412413
IsHitTestVisible="False"
413414
MaxLines="1"
414415
RenderTransformOrigin="0,0.5"
415-
Style="{StaticResource MaterialBodyLarge}"
416+
Style="{ThemeResource ComboBoxPlaceholderTextStyle}"
416417
Text="{TemplateBinding PlaceholderText}">
417418
<TextBlock.RenderTransform>
418419
<CompositeTransform x:Name="UpperPlaceholderElement_CompositeTransform"
419-
ScaleX="0.7"
420-
ScaleY="0.7"
421-
TranslateY="-11" />
420+
ScaleX="{ThemeResource ComboBoxUpperPlaceHolderScaleX}"
421+
ScaleY="{ThemeResource ComboBoxUpperPlaceHolderScaleY}"
422+
TranslateY="{ThemeResource ComboBoxUpperPlaceHolderTranslateY}" />
422423
</TextBlock.RenderTransform>
423424
</TextBlock>
424425

‎src/library/Uno.Material/Styles/Controls/v2/SemanticStyles.xaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@
429429
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundFocused" ResourceKey="PrimaryBrush" />
430430
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundFocusedPressed" ResourceKey="PrimaryBrush" />
431431
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundOpened" ResourceKey="PrimaryBrush" />
432+
<StaticResource x:Key="ComboBoxPlaceholderTextStyle" ResourceKey="MaterialBodyLarge" />
433+
<!-- Floating label: the placeholder scaled down and moved above the selected value -->
434+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleX">0.7</x:Double>
435+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleY">0.7</x:Double>
436+
<x:Double x:Key="ComboBoxUpperPlaceHolderTranslateY">-11</x:Double>
437+
<Thickness x:Key="ComboBoxUpperPlaceHolderMargin">0</Thickness>
432438
<StaticResource x:Key="ComboBoxCornerRadius" ResourceKey="Radius100CornerRadius" />
433439
<x:Double x:Key="ComboBoxHeight">50</x:Double>
434440
<x:Double x:Key="ComboBoxMinHeight">56</x:Double>
@@ -1469,6 +1475,12 @@
14691475
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundFocused" ResourceKey="PrimaryBrush" />
14701476
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundFocusedPressed" ResourceKey="PrimaryBrush" />
14711477
<StaticResource x:Key="ComboBoxUpperPlaceHolderForegroundOpened" ResourceKey="PrimaryBrush" />
1478+
<StaticResource x:Key="ComboBoxPlaceholderTextStyle" ResourceKey="MaterialBodyLarge" />
1479+
<!-- Floating label: the placeholder scaled down and moved above the selected value -->
1480+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleX">0.7</x:Double>
1481+
<x:Double x:Key="ComboBoxUpperPlaceHolderScaleY">0.7</x:Double>
1482+
<x:Double x:Key="ComboBoxUpperPlaceHolderTranslateY">-11</x:Double>
1483+
<Thickness x:Key="ComboBoxUpperPlaceHolderMargin">0</Thickness>
14721484
<StaticResource x:Key="ComboBoxCornerRadius" ResourceKey="Radius100CornerRadius" />
14731485
<x:Double x:Key="ComboBoxHeight">50</x:Double>
14741486
<x:Double x:Key="ComboBoxMinHeight">56</x:Double>

‎src/library/Uno.Themes.WinUI.Markup/Theme/ComboBox.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ public static partial class UpGlyph
190190
public static ThemeResourceKey<double> Width => new("ComboBoxUpGlyphWidth");
191191
}
192192

193+
public static partial class UpperPlaceHolder
194+
{
195+
[ResourceKeyDefinition(typeof(Thickness), "ComboBoxUpperPlaceHolderMargin")]
196+
public static ThemeResourceKey<Thickness> Margin => new("ComboBoxUpperPlaceHolderMargin");
197+
198+
[ResourceKeyDefinition(typeof(double), "ComboBoxUpperPlaceHolderScaleX")]
199+
public static ThemeResourceKey<double> ScaleX => new("ComboBoxUpperPlaceHolderScaleX");
200+
201+
[ResourceKeyDefinition(typeof(double), "ComboBoxUpperPlaceHolderScaleY")]
202+
public static ThemeResourceKey<double> ScaleY => new("ComboBoxUpperPlaceHolderScaleY");
203+
204+
[ResourceKeyDefinition(typeof(double), "ComboBoxUpperPlaceHolderTranslateY")]
205+
public static ThemeResourceKey<double> TranslateY => new("ComboBoxUpperPlaceHolderTranslateY");
206+
}
207+
193208
public static partial class UpperPlaceHolderForeground
194209
{
195210
[ResourceKeyDefinition(typeof(Brush), "ComboBoxUpperPlaceHolderForeground")]
@@ -226,6 +241,9 @@ public static partial class UpperPlaceHolderForeground
226241
[ResourceKeyDefinition(typeof(Thickness), "ComboBoxPadding")]
227242
public static ThemeResourceKey<Thickness> Padding => new("ComboBoxPadding");
228243

244+
[ResourceKeyDefinition(typeof(Style), "ComboBoxPlaceholderTextStyle", TargetType = typeof(global::Microsoft.UI.Xaml.Controls.TextBlock))]
245+
public static ThemeResourceKey<Style> PlaceholderTextStyle => new("ComboBoxPlaceholderTextStyle");
246+
229247
[ResourceKeyDefinition(typeof(string), "ComboBoxUpArrowPathData")]
230248
public static StaticResourceKey<string> UpArrowPathData => new("ComboBoxUpArrowPathData");
231249
}

0 commit comments

Comments
 (0)