1+ namespace MADE . UI . Controls
2+ {
3+ using Windows . UI . Text ;
4+ using Windows . UI . Xaml ;
5+ using Windows . UI . Xaml . Controls . Primitives ;
6+ using Extensions ;
7+
8+ /// <summary>
9+ /// Defines the list style logic for the <see cref="RichEditToolbar"/>.
10+ /// </summary>
11+ public partial class RichEditToolbar
12+ {
13+ /// <summary>
14+ /// Identifies the <see cref="ShowListStyleOptions"/> dependency property.
15+ /// </summary>
16+ public static readonly DependencyProperty ShowListStyleOptionsProperty = DependencyProperty . Register (
17+ nameof ( ShowListStyleOptions ) ,
18+ typeof ( bool ) ,
19+ typeof ( RichEditToolbar ) ,
20+ new PropertyMetadata ( true , ( o , args ) => ( ( RichEditToolbar ) o ) . UpdateListStyleOptionsVisibility ( ) ) ) ;
21+
22+ private const string RichEditToolbarBulletListButtonPart = "RichEditToolbarBulletListButton" ;
23+ private const string RichEditToolbarNumberListButtonPart = "RichEditToolbarNumberListButton" ;
24+
25+ /// <summary>
26+ /// Occurs when the list style has changed.
27+ /// </summary>
28+ public event RichEditToolbarListStyleChangedEventHandler ListStyleChanged ;
29+
30+ /// <summary>
31+ /// Gets or sets a value indicating whether to show list style options.
32+ /// </summary>
33+ public bool ShowListStyleOptions
34+ {
35+ get => ( bool ) GetValue ( ShowListStyleOptionsProperty ) ;
36+ set => SetValue ( ShowListStyleOptionsProperty , value ) ;
37+ }
38+
39+ /// <summary>
40+ /// Gets the view representing the button for toggling bullet list.
41+ /// </summary>
42+ public ToggleButton BulletListButton { get ; private set ; }
43+
44+ /// <summary>
45+ /// Gets the view representing the button for toggling number list.
46+ /// </summary>
47+ public ToggleButton NumberListButton { get ; private set ; }
48+
49+ private void SetupListStyleOptions ( )
50+ {
51+ this . BulletListButton = this . GetChildView < ToggleButton > ( RichEditToolbarBulletListButtonPart ) ;
52+ this . NumberListButton = this . GetChildView < ToggleButton > ( RichEditToolbarNumberListButtonPart ) ;
53+
54+ this . UpdateListStyleOptionsVisibility ( ) ;
55+
56+ if ( this . BulletListButton != null )
57+ {
58+ this . BulletListButton . Checked += this . OnBulletListButtonChecked ;
59+ this . BulletListButton . Unchecked += this . OnBulletListButtonChecked ;
60+ }
61+
62+ if ( this . NumberListButton != null )
63+ {
64+ this . NumberListButton . Checked += this . OnNumberListButtonChecked ;
65+ this . NumberListButton . Unchecked += this . OnNumberListButtonChecked ;
66+ }
67+ }
68+
69+ private void UpdateListStyleOptionsVisibility ( )
70+ {
71+ this . BulletListButton ? . SetVisible ( this . ShowListStyleOptions ) ;
72+ this . NumberListButton ? . SetVisible ( this . ShowListStyleOptions ) ;
73+ }
74+
75+ #if WINDOWS_UWP
76+ private void UpdateActiveListStyleOptions ( )
77+ {
78+ var activeListStyleOption = this . TargetRichEditBox
79+ . Document
80+ . Selection
81+ . ParagraphFormat
82+ . ListType ;
83+
84+ if ( activeListStyleOption == MarkerType . Undefined )
85+ {
86+ return ;
87+ }
88+
89+ if ( this . BulletListButton != null )
90+ {
91+ this . BulletListButton . IsChecked = activeListStyleOption == MarkerType . Bullet ;
92+ }
93+
94+ if ( this . NumberListButton != null )
95+ {
96+ this . NumberListButton . IsChecked = activeListStyleOption == MarkerType . CircledNumber ;
97+ }
98+
99+ this . EmitListStyleChanged ( ) ;
100+ }
101+ #endif
102+
103+ private void ResetListStyleOptions ( )
104+ {
105+ if ( this . BulletListButton != null )
106+ {
107+ this . BulletListButton . Checked -= this . OnBulletListButtonChecked ;
108+ this . BulletListButton . Unchecked -= this . OnBulletListButtonChecked ;
109+ }
110+
111+ if ( this . NumberListButton != null )
112+ {
113+ this . NumberListButton . Checked -= this . OnNumberListButtonChecked ;
114+ this . NumberListButton . Unchecked -= this . OnNumberListButtonChecked ;
115+ }
116+ }
117+
118+ private void OnNumberListButtonChecked ( object sender , RoutedEventArgs e )
119+ {
120+ if ( this . NumberListButton == null
121+ #if WINDOWS_UWP
122+ || this . TargetRichEditBox == null
123+ #endif
124+ )
125+ {
126+ return ;
127+ }
128+
129+ #if WINDOWS_UWP
130+ var isChecked = this . NumberListButton . IsChecked ?? false ;
131+ var isBulletListChecked = this . BulletListButton ? . IsChecked ?? false ;
132+
133+ // Resets the bullet list check option.
134+ if ( isChecked && isBulletListChecked && this . BulletListButton != null )
135+ {
136+ this . BulletListButton . Unchecked -= this . OnBulletListButtonChecked ;
137+ this . BulletListButton . IsChecked = false ;
138+ this . BulletListButton . Unchecked += this . OnBulletListButtonChecked ;
139+ }
140+
141+ this . TargetRichEditBox
142+ . Document
143+ . Selection
144+ . ParagraphFormat . ListType =
145+ isChecked ? MarkerType . CircledNumber : MarkerType . None ;
146+
147+ this . TargetRichEditBox . Document . Selection . ParagraphFormat . ListStyle = MarkerStyle . Plain ;
148+ #endif
149+
150+ this . EmitListStyleChanged ( ) ;
151+ }
152+
153+ private void OnBulletListButtonChecked ( object sender , RoutedEventArgs e )
154+ {
155+ if ( this . BulletListButton == null
156+ #if WINDOWS_UWP
157+ || this . TargetRichEditBox == null
158+ #endif
159+ )
160+ {
161+ return ;
162+ }
163+
164+ #if WINDOWS_UWP
165+ var isChecked = this . BulletListButton . IsChecked ?? false ;
166+ var isNumberListChecked = this . NumberListButton ? . IsChecked ?? false ;
167+
168+ // Resets the number list check option.
169+ if ( isChecked && isNumberListChecked && this . NumberListButton != null )
170+ {
171+ this . NumberListButton . Unchecked -= this . OnNumberListButtonChecked ;
172+ this . NumberListButton . IsChecked = false ;
173+ this . NumberListButton . Unchecked += this . OnNumberListButtonChecked ;
174+ }
175+
176+ this . TargetRichEditBox
177+ . Document
178+ . Selection
179+ . ParagraphFormat . ListType =
180+ isChecked ? MarkerType . Bullet : MarkerType . None ;
181+ #endif
182+
183+ this . EmitListStyleChanged ( ) ;
184+ }
185+
186+ private void EmitListStyleChanged ( )
187+ {
188+ if ( this . ListStyleChanged == null )
189+ {
190+ return ;
191+ }
192+
193+ var numberedList = this . NumberListButton ? . IsChecked ?? false ;
194+ var bulletedList = this . BulletListButton ? . IsChecked ?? false ;
195+
196+ this . ListStyleChanged . Invoke (
197+ this ,
198+ new RichEditToolbarListStyleChangedEventArgs ( bulletedList , numberedList ) ) ;
199+ }
200+ }
201+ }
0 commit comments