[NUI] Do not set value of property again when theme is changed.
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ThemeChangeSample.cs
1 using System;
2 using System.Linq;
3 using Tizen.NUI.Components;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.Applications.ThemeManager;
6
7 namespace Tizen.NUI.Samples
8 {
9     public class ThemeChangeSample : IExample
10     {
11         private ThemeLoader themeLoader;
12
13         public void Activate()
14         {
15             themeLoader = new ThemeLoader();
16
17             var mainPage = new ContentPage()
18             {
19                 ThemeChangeSensitive = true,
20                 AppBar = new AppBar()
21                 {
22                     AutoNavigationContent = false,
23                     Title = "NUI theme sample",
24                     //Actions = new View[] { closeButton },
25                 },
26                 Content = new ScrollableBase()
27                 {
28                     Layout = new LinearLayout()
29                     {
30                         LinearOrientation = LinearLayout.Orientation.Vertical,
31                         CellPadding = new Size2D(20, 20)
32                     },
33                     WidthResizePolicy = ResizePolicyType.FillToParent,
34                     HeightResizePolicy = ResizePolicyType.FillToParent,
35                     Padding = new Extents(30, 30, 20, 20)
36                 }
37             };
38
39             string[] ids = (string[])themeLoader.QueryIds();
40             if (ids != null && ids.Contains("org.tizen.default-dark-theme") || ids.Contains("org.tizen.default-light-theme"))
41             {
42                 var title = $"Current theme: {(themeLoader.CurrentTheme.Id == "org.tizen.default-dark-theme" ? "Dark" : "Light")}";
43
44                 mainPage.Content.Add(CreateClickableItem("Theme change", title, delegate (View view)
45                 {
46                     TextLabel text = view.Children[1] as TextLabel;
47
48                     if (themeLoader.CurrentTheme.Id == "org.tizen.default-dark-theme")
49                     {
50                         var theme = themeLoader.LoadTheme("org.tizen.default-light-theme");
51                         Tizen.Log.Info("test", $"Id: {theme.Id}, Version: {theme.Version}");
52                         themeLoader.CurrentTheme = theme;
53                         text.Text = "Current theme: Light";
54                     }
55                     else
56                     {
57                         var theme = themeLoader.LoadTheme("org.tizen.default-dark-theme");
58                         Tizen.Log.Info("test", $"Id: {theme.Id}, Version: {theme.Version}");
59                         themeLoader.CurrentTheme = theme;
60                         text.Text = "Current theme: Dark";
61                     }
62                 }));
63
64                 mainPage.Content.Add(CreateItem("Switch", CreateSwitchExample()));
65             }
66             else
67             {
68                 mainPage.AppBar.Title = "No proper theme is found!";
69             }
70
71             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Push(mainPage);
72         }
73
74         public void Deactivate()
75         {
76             NUIApplication.GetDefaultWindow().GetDefaultNavigator().Pop();
77
78             themeLoader.Dispose();
79             themeLoader = null;
80         }
81
82         private View CreateItem(string title, View content)
83         {
84             var item = new View()
85             {
86                 WidthResizePolicy = ResizePolicyType.FillToParent,
87                 HeightResizePolicy = ResizePolicyType.FitToChildren,
88                 Layout = new LinearLayout()
89                 {
90                     LinearOrientation = LinearLayout.Orientation.Vertical,
91                 },
92                 BackgroundColor = new Color("#88888860"),
93                 CornerRadius = 16.0f,
94                 Padding = 20,
95             };
96             item.Add(new TextLabel()
97             {
98                 PixelSize = 22.0f,
99                 Text = title,
100                 Padding = new Extents(0, 0, 0, 20)
101             });
102             item.Add(content);
103             return item;
104         }
105
106         private View CreateClickableItem(string title, string subtitle, Action<View> clicked)
107         {
108             var item = new View()
109             {
110                 WidthResizePolicy = ResizePolicyType.FillToParent,
111                 HeightResizePolicy = ResizePolicyType.FitToChildren,
112                 Layout = new LinearLayout()
113                 {
114                     LinearOrientation = LinearLayout.Orientation.Vertical,
115                 },
116                 BackgroundColor = new Color("#88888860"),
117                 CornerRadius = 16.0f,
118                 Padding = 20,
119                 LeaveRequired = true,
120             };
121             item.TouchEvent += (s, e) => {
122                 var state = e.Touch.GetState(0);
123                 if (state == PointStateType.Down)
124                 {
125                     ((View)s).BackgroundColor = new Color("#888888BB");
126                 }
127                 else if (state == PointStateType.Up)
128                 {
129                     ((View)s).BackgroundColor = new Color("#88888860");
130                     clicked?.Invoke(item);
131                 }
132                 else if (state == PointStateType.Leave || state == PointStateType.Interrupted)
133                 {
134                     ((View)s).BackgroundColor = new Color("#88888860");
135                 }
136                 return true;
137             };
138             item.Add(new TextLabel()
139             {
140                 PixelSize = 22.0f,
141                 Text = title,
142                 Padding = new Extents(0, 0, 0, 20)
143             });
144             item.Add(new TextLabel()
145             {
146                 PixelSize = 32.0f,
147                 Text = subtitle,
148                 VerticalAlignment = VerticalAlignment.Center,
149             });
150             return item;
151         }
152
153         private View CreateSwitchExample()
154         {
155             var view = new View()
156             {
157                 WidthResizePolicy = ResizePolicyType.FillToParent,
158                 HeightResizePolicy = ResizePolicyType.FitToChildren,
159             };
160             var textLabel = new TextLabel()
161             {
162                 Text = "Auto update : on",
163                 TextColor = Color.Red,
164             };
165             view.Add(textLabel);
166
167             var switchButton = new Switch()
168             {
169                 ParentOrigin = ParentOrigin.CenterRight,
170                 PivotPoint = PivotPoint.CenterRight,
171                 PositionUsesPivotPoint = true,
172                 IsSelected = true,
173             };
174             switchButton.SelectedChanged += (s, e) => {
175                 textLabel.Text = $"Daily auto update : {(((Switch)s).IsSelected ? "on" : "off")}";
176             };
177             view.Add(switchButton);
178             return view;
179         }
180     }
181 }