-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
BaseComponent.cs
184 lines (172 loc) · 6.53 KB
/
BaseComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#region copyright
// Copyright (C) 2022 Auto Dark Mode
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion
using AutoDarkModeSvc.Interfaces;
using System;
using AutoDarkModeLib;
using AutoDarkModeLib.Interfaces;
using AutoDarkModeSvc.Events;
using AutoDarkModeSvc.Monitors;
using AutoDarkModeSvc.Core;
using Windows.ApplicationModel.VoiceCommands;
namespace AutoDarkModeSvc.SwitchComponents
{
abstract class BaseComponent<T> : ISwitchComponent
{
protected NLog.Logger Logger { get; private set; }
protected GlobalState GlobalState { get; } = GlobalState.Instance();
protected ISwitchComponentSettings<T> Settings { get; set; }
protected ISwitchComponentSettings<T> SettingsBefore { get; set; }
public bool Initialized { get; private set; }
public BaseComponent()
{
Logger = NLog.LogManager.GetLogger(GetType().ToString());
}
public virtual DwmRefreshType TriggersDwmRefresh { get; protected set; } = DwmRefreshType.None;
public virtual DwmRefreshType NeedsDwmRefresh { get; protected set; } = DwmRefreshType.None;
public virtual int PriorityToLight { get; }
public virtual int PriorityToDark { get; }
public virtual HookPosition HookPosition { get; protected set; } = HookPosition.PostSync;
public bool ForceSwitch { get; set; }
public virtual bool Enabled
{
get { return Settings.Enabled; }
}
public void Switch(SwitchEventArgs e)
{
Logger.Trace($"switch invoked for {GetType().Name} ({Enum.GetName(HookPosition)})");
ForceSwitch = false;
if (Enabled)
{
if (!Initialized)
{
RunEnableHook();
}
try
{
HandleSwitch(e);
}
catch (Exception ex)
{
Logger.Error(ex, $"uncaught exception in component {GetType().Name}, source: {ex.Source}, message: ");
}
}
else if (Initialized)
{
RunDisableHook();
}
}
protected virtual void UpdateSettingsState() { }
/// <summary>
/// Initializes the module if it has a hook specified. Does nothing otherwise.
/// </summary>
protected virtual void EnableHook() { }
/// <summary>
/// Deinitializes the module and restores the original state. Does nothing if no hook is specified.
/// </summary>
protected virtual void DisableHook() { }
/// <summary>
/// A callback method that is invoked after the component has run to its completion and a theme switch was performed.
/// Adm at this point is in a stable state with the new theme settings being available
/// </summary>
protected virtual void Callback(SwitchEventArgs e) { }
/// <summary>
/// True when the component should be compatible with the ThemeHandler switching mode
/// </summary>
public abstract bool ThemeHandlerCompatibility { get; }
/// <summary>
/// Entrypoint, called when a component needs to be updated
/// </summary>
/// <param name="newTheme">the new theme to apply</param>
/// <param name="e">the switch event args</param>
protected abstract void HandleSwitch(SwitchEventArgs e);
/// <summary>
/// Determines whether the component needs to be triggered to update to the correct system state
/// </summary>
/// <returns>true if the component needs to be executed; false otherwise</returns>
protected abstract bool ComponentNeedsUpdate(SwitchEventArgs e);
/// <summary>
/// Executes the update settings state method
/// </summary>
/// <param name="newSettings"></param>
public void RunUpdateSettingsState(object newSettings)
{
if (newSettings is ISwitchComponentSettings<T> temp)
{
bool isInit = Settings == null;
SettingsBefore = Settings;
Settings = temp;
if (!isInit) UpdateSettingsState();
}
else
{
Logger.Error($"could not convert generic settings object to ${typeof(T)}, no settings update performed.");
}
}
/// <summary>
/// Executes the callback method
/// </summary>
public void RunCallback(SwitchEventArgs e)
{
Logger.Trace($"running callback for {GetType().Name}");
Callback(e);
}
/// <summary>
/// Executes the enable hook
/// </summary>
public void RunEnableHook()
{
Logger.Debug($"running enable hook for {GetType().Name}");
try
{
EnableHook();
}
catch (Exception ex)
{
Logger.Error(ex, $"error while running enable hook for {GetType().Name}");
}
Initialized = true;
}
/// <summary>
/// Executes the disable hook
/// </summary>
public void RunDisableHook()
{
Logger.Debug($"running disable hook for {GetType().Name}");
try
{
DisableHook();
}
catch (Exception ex)
{
Logger.Error(ex, $"error while running disable hook for {GetType().Name}");
}
Initialized = false;
}
public bool RunComponentNeedsUpdate(SwitchEventArgs e)
{
try
{
return ComponentNeedsUpdate(e);
}
catch (Exception ex)
{
Logger.Error(ex, $"uncaught exception in component {GetType().Name}'s update rule, source: {ex.Source}, message: ");
}
return false;
}
}
}