11using System ;
2- using System . Linq ;
32using System . Runtime . CompilerServices ;
43using System . Threading . Tasks ;
54using System . Windows . Input ;
6- using Plugin . MaterialDesignControls . Effects ;
7- using Plugin . MaterialDesignControls . Implementations ;
85using Xamarin . Forms ;
96
107namespace Plugin . MaterialDesignControls
@@ -27,9 +24,13 @@ public MaterialButton()
2724
2825 private bool initialized = false ;
2926
30- protected CustomButton button ;
27+ protected Frame frmLayout ;
3128
32- private Grid grid ;
29+ private StackLayout stcLayout ;
30+
31+ private Image imgIcon ;
32+
33+ private MaterialLabel lblText ;
3334
3435 private ActivityIndicator actIndicator ;
3536
@@ -216,30 +217,51 @@ private void Initialize()
216217 {
217218 this . initialized = true ;
218219
219- this . grid = new Grid
220+ this . frmLayout = new Frame
220221 {
222+ HasShadow = false ,
223+ CornerRadius = 4 ,
221224 MinimumHeightRequest = 40 ,
222225 HeightRequest = 40 ,
226+ Padding = new Thickness ( 12 , 0 )
223227 } ;
224- this . Content = this . grid ;
228+ this . Content = this . frmLayout ;
225229
226- this . actIndicator = new ActivityIndicator
230+ this . stcLayout = new StackLayout
227231 {
228- VerticalOptions = LayoutOptions . Center ,
232+ Orientation = StackOrientation . Horizontal ,
233+ Spacing = 12 ,
229234 HorizontalOptions = LayoutOptions . Center ,
235+ } ;
236+ this . frmLayout . Content = this . stcLayout ;
237+
238+ this . imgIcon = new Image
239+ {
240+ VerticalOptions = LayoutOptions . Center ,
230241 WidthRequest = 24 ,
231242 HeightRequest = 24 ,
232- IsRunning = false ,
233243 IsVisible = false
234244 } ;
235- this . grid . Children . Add ( this . actIndicator , 0 , 0 ) ;
245+ this . stcLayout . Children . Add ( this . imgIcon ) ;
236246
237- this . button = new CustomButton
247+ this . lblText = new MaterialLabel
238248 {
239- CornerRadius = 4 ,
240- Padding = new Thickness ( 12 , 0 )
249+ LineBreakMode = LineBreakMode . NoWrap ,
250+ VerticalOptions = LayoutOptions . Center
241251 } ;
242- this . grid . Children . Add ( this . button , 0 , 0 ) ;
252+ this . stcLayout . Children . Add ( this . lblText ) ;
253+
254+ this . actIndicator = new ActivityIndicator
255+ {
256+ VerticalOptions = LayoutOptions . Center ,
257+ HorizontalOptions = LayoutOptions . Center ,
258+ WidthRequest = 24 ,
259+ HeightRequest = 24 ,
260+ IsVisible = false
261+ } ;
262+ this . stcLayout . Children . Add ( this . actIndicator ) ;
263+
264+ this . Effects . Add ( new TouchAndPressEffect ( ) ) ;
243265 }
244266
245267 protected override void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
@@ -257,43 +279,43 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
257279 break ;
258280 case nameof ( this . Text ) :
259281 case nameof ( this . ToUpper ) :
260- this . button . Text = this . ToUpper ? this . Text ? . ToUpper ( ) : this . Text ;
282+ this . lblText . Text = this . ToUpper ? this . Text ? . ToUpper ( ) : this . Text ;
261283 break ;
262284 case nameof ( this . TextColor ) :
263285 case nameof ( this . DisabledTextColor ) :
264- this . button . TextColor = this . IsEnabled ? this . TextColor : this . DisabledTextColor ;
286+ this . lblText . TextColor = this . IsEnabled ? this . TextColor : this . DisabledTextColor ;
265287 break ;
266288 case nameof ( this . TextSize ) :
267- this . button . FontSize = this . TextSize ;
289+ this . lblText . FontSize = this . TextSize ;
268290 break ;
269291 case nameof ( this . FontFamily ) :
270- this . button . FontFamily = this . FontFamily ;
292+ this . lblText . FontFamily = this . FontFamily ;
271293 break ;
272294 case nameof ( this . CornerRadius ) :
273- this . button . CornerRadius = Convert . ToInt32 ( this . CornerRadius ) ;
295+ this . frmLayout . CornerRadius = Convert . ToInt32 ( this . CornerRadius ) ;
274296 break ;
275297 case nameof ( this . BackgroundColor ) :
276298 case nameof ( this . DisabledBackgroundColor ) :
277- this . button . BackgroundColor = this . IsEnabled ? this . BackgroundColor : this . DisabledBackgroundColor ;
299+ this . frmLayout . BackgroundColor = this . IsEnabled ? this . BackgroundColor : this . DisabledBackgroundColor ;
278300 break ;
279301 case nameof ( this . BorderColor ) :
280302 case nameof ( this . DisabledBorderColor ) :
281- this . button . BorderColor = this . IsEnabled ? this . BorderColor : this . DisabledBorderColor ;
303+ this . frmLayout . BorderColor = this . IsEnabled ? this . BorderColor : this . DisabledBorderColor ;
282304 break ;
283305 case nameof ( this . Icon ) :
284306 case nameof ( this . DisabledIcon ) :
285307 if ( ! string . IsNullOrEmpty ( this . Icon ) || ! string . IsNullOrEmpty ( this . DisabledIcon ) )
286308 {
287- this . button . ImageSource = this . IsEnabled ? this . Icon : this . DisabledIcon ;
309+ this . imgIcon . Source = this . IsEnabled ? this . Icon : this . DisabledIcon ;
288310 }
289311 break ;
290312 case nameof ( this . IsEnabled ) :
291- this . button . TextColor = this . IsEnabled ? this . TextColor : this . DisabledTextColor ;
292- this . button . BackgroundColor = this . IsEnabled ? this . BackgroundColor : this . DisabledBackgroundColor ;
293- this . button . BorderColor = this . IsEnabled ? this . BorderColor : this . DisabledBorderColor ;
313+ this . lblText . TextColor = this . IsEnabled ? this . TextColor : this . DisabledTextColor ;
314+ this . frmLayout . BackgroundColor = this . IsEnabled ? this . BackgroundColor : this . DisabledBackgroundColor ;
315+ this . frmLayout . BorderColor = this . IsEnabled ? this . BorderColor : this . DisabledBorderColor ;
294316 if ( ! string . IsNullOrEmpty ( this . Icon ) || ! string . IsNullOrEmpty ( this . DisabledIcon ) )
295317 {
296- this . button . ImageSource = this . IsEnabled ? this . Icon : this . DisabledIcon ;
318+ this . imgIcon . Source = this . IsEnabled ? this . Icon : this . DisabledIcon ;
297319 }
298320 break ;
299321 case nameof ( this . BusyColor ) :
@@ -302,39 +324,21 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
302324 case nameof ( this . IsBusy ) :
303325 if ( this . IsBusy )
304326 {
327+ this . lblText . IsVisible = false ;
328+ this . imgIcon . IsVisible = false ;
305329 this . actIndicator . IsVisible = true ;
306330 this . actIndicator . IsRunning = true ;
307- this . button . IsVisible = false ;
331+ this . frmLayout . BackgroundColor = Color . Transparent ;
332+ this . frmLayout . BorderColor = Color . Transparent ;
308333 }
309334 else
310335 {
336+ this . lblText . IsVisible = true ;
337+ this . imgIcon . IsVisible = ! string . IsNullOrEmpty ( this . Icon ) || ! string . IsNullOrEmpty ( this . DisabledIcon ) ;
311338 this . actIndicator . IsVisible = false ;
312339 this . actIndicator . IsRunning = false ;
313- this . button . IsVisible = true ;
314- }
315- break ;
316- case nameof ( this . Command ) :
317- if ( this . Animation == AnimationTypes . None )
318- {
319- this . button . Command = this . Command ;
320- }
321- break ;
322- case nameof ( this . CommandParameter ) :
323- if ( this . Animation == AnimationTypes . None )
324- {
325- this . button . CommandParameter = this . CommandParameter ;
326- }
327- break ;
328- case nameof ( this . Animation ) :
329- if ( this . Animation != AnimationTypes . None )
330- {
331- this . button . Command = null ;
332- this . button . CommandParameter = null ;
333-
334- if ( ! this . Effects . Any ( ) )
335- {
336- this . Effects . Add ( new TouchAndPressEffect ( ) ) ;
337- }
340+ this . frmLayout . BackgroundColor = this . IsEnabled ? this . BackgroundColor : this . DisabledBackgroundColor ;
341+ this . frmLayout . BorderColor = this . IsEnabled ? this . BorderColor : this . DisabledBorderColor ;
338342 }
339343 break ;
340344 }
@@ -345,7 +349,7 @@ public void ConsumeEvent(EventType gestureType)
345349 switch ( gestureType )
346350 {
347351 case EventType . Pressing :
348- if ( this . IsEnabled && ( this . Command == null || this . Command . CanExecute ( this . CommandParameter ) ) )
352+ if ( this . Animation != AnimationTypes . None && this . IsEnabled && ( this . Command == null || this . Command . CanExecute ( this . CommandParameter ) ) )
349353 {
350354 Task . Run ( async ( ) =>
351355 {
@@ -367,17 +371,20 @@ public void ConsumeEvent(EventType gestureType)
367371 this . Command . Execute ( this . CommandParameter ) ;
368372 }
369373
370- Task . Run ( async ( ) =>
374+ if ( this . Animation != AnimationTypes . None )
371375 {
372- if ( this . Animation == AnimationTypes . Fade )
373- {
374- await this . FadeTo ( 1 , 100 ) ;
375- }
376- else
376+ Task . Run ( async ( ) =>
377377 {
378- await this . ScaleTo ( 1 , 100 ) ;
379- }
380- } ) ;
378+ if ( this . Animation == AnimationTypes . Fade )
379+ {
380+ await this . FadeTo ( 1 , 100 ) ;
381+ }
382+ else
383+ {
384+ await this . ScaleTo ( 1 , 100 ) ;
385+ }
386+ } ) ;
387+ }
381388 break ;
382389 default :
383390 throw new ArgumentOutOfRangeException ( nameof ( gestureType ) , gestureType , null ) ;
0 commit comments