1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-28 17:34:06 +02:00

Update code to use C# 8 switch expression

This commit is contained in:
chylex 2019-05-27 16:04:08 +02:00
parent b2937bc776
commit dd8c5d27be
10 changed files with 89 additions and 109 deletions

View File

@ -119,14 +119,12 @@ public int GetIdleSeconds(){
} }
public void Alert(string type, string contents){ public void Alert(string type, string contents){
MessageBoxIcon icon; MessageBoxIcon icon = type switch{
"error" => MessageBoxIcon.Error,
switch(type){ "warning" => MessageBoxIcon.Warning,
case "error": icon = MessageBoxIcon.Error; break; "info" => MessageBoxIcon.Information,
case "warning": icon = MessageBoxIcon.Warning; break; _ => MessageBoxIcon.None
case "info": icon = MessageBoxIcon.Information; break; };
default: icon = MessageBoxIcon.None; break;
}
FormMessage.Show("TweetDuck Browser Message", contents, icon, FormMessage.OK); FormMessage.Show("TweetDuck Browser Message", contents, icon, FormMessage.OK);
} }

View File

@ -12,15 +12,17 @@ private static FormMessage CreateMessageForm(string caption, string text){
int pipe = text.IndexOf('|'); int pipe = text.IndexOf('|');
if (pipe != -1){ if (pipe != -1){
switch(text.Substring(0, pipe)){ icon = text.Substring(0, pipe) switch{
case "error": icon = MessageBoxIcon.Error; break; "error" => MessageBoxIcon.Error,
case "warning": icon = MessageBoxIcon.Warning; break; "warning" => MessageBoxIcon.Warning,
case "info": icon = MessageBoxIcon.Information; break; "info" => MessageBoxIcon.Information,
case "question": icon = MessageBoxIcon.Question; break; "question" => MessageBoxIcon.Question,
default: return new FormMessage(caption, text, icon); _ => MessageBoxIcon.None
} };
text = text.Substring(pipe+1); if (icon != MessageBoxIcon.None){
text = text.Substring(pipe + 1);
}
} }
return new FormMessage(caption, text, icon); return new FormMessage(caption, text, icon);

View File

@ -14,15 +14,13 @@ abstract partial class FormNotificationBase : Form, AnalyticsFile.IProvider{
protected static UserConfig Config => Program.Config.User; protected static UserConfig Config => Program.Config.User;
protected static int FontSizeLevel{ protected static int FontSizeLevel{
get{ get => TweetDeckBridge.FontSize switch{
switch(TweetDeckBridge.FontSize){ "largest" => 4,
case "largest": return 4; "large" => 3,
case "large": return 3; "small" => 1,
case "small": return 1; "smallest" => 0,
case "smallest": return 0; _ => 2
default: return 2; };
}
}
} }
protected virtual Point PrimaryLocation{ protected virtual Point PrimaryLocation{

View File

@ -44,27 +44,17 @@ public virtual bool RequiresResize{
} }
private int BaseClientWidth{ private int BaseClientWidth{
get{ get => Config.NotificationSize switch{
switch(Config.NotificationSize){ TweetNotification.Size.Custom => Config.CustomNotificationSize.Width,
default: _ => BrowserUtils.Scale(284, SizeScale * (1.0 + 0.05 * FontSizeLevel))
return BrowserUtils.Scale(284, SizeScale*(1.0+0.05*FontSizeLevel)); };
case TweetNotification.Size.Custom:
return Config.CustomNotificationSize.Width;
}
}
} }
private int BaseClientHeight{ private int BaseClientHeight{
get{ get => Config.NotificationSize switch{
switch(Config.NotificationSize){ TweetNotification.Size.Custom => Config.CustomNotificationSize.Height,
default: _ => BrowserUtils.Scale(122, SizeScale * (1.0 + 0.08 * FontSizeLevel))
return BrowserUtils.Scale(122, SizeScale*(1.0+0.08*FontSizeLevel)); };
case TweetNotification.Size.Custom:
return Config.CustomNotificationSize.Height;
}
}
} }
protected virtual string BodyClasses => IsCursorOverBrowser ? "td-notification td-hover" : "td-notification"; protected virtual string BodyClasses => IsCursorOverBrowser ? "td-notification td-hover" : "td-notification";

View File

@ -11,18 +11,16 @@ static class SoundNotification{
public const string SupportedFormats = "*.wav;*.ogg;*.mp3;*.flac;*.opus;*.weba;*.webm"; public const string SupportedFormats = "*.wav;*.ogg;*.mp3;*.flac;*.opus;*.weba;*.webm";
public static IResourceHandler CreateFileHandler(string path){ public static IResourceHandler CreateFileHandler(string path){
string mimeType; string mimeType = Path.GetExtension(path) switch{
".weba" => "audio/webm",
switch(Path.GetExtension(path)){ ".webm" => "audio/webm",
case ".weba": ".wav" => "audio/wav",
case ".webm": mimeType = "audio/webm"; break; ".ogg" => "audio/ogg",
case ".wav": mimeType = "audio/wav"; break; ".mp3" => "audio/mp3",
case ".ogg": mimeType = "audio/ogg"; break; ".flac" => "audio/flac",
case ".mp3": mimeType = "audio/mp3"; break; ".opus" => "audio/ogg; codecs=opus",
case ".flac": mimeType = "audio/flac"; break; _ => null
case ".opus": mimeType = "audio/ogg; codecs=opus"; break; };
default: mimeType = null; break;
}
try{ try{
return ResourceHandler.FromFilePath(path, mimeType); return ResourceHandler.FromFilePath(path, mimeType);

View File

@ -207,36 +207,30 @@ private static string CustomBrowser{
} }
private static string TrayMode{ private static string TrayMode{
get{ get => UserConfig.TrayBehavior switch{
switch(UserConfig.TrayBehavior){ TrayIcon.Behavior.DisplayOnly => "icon",
case TrayIcon.Behavior.DisplayOnly: return "icon"; TrayIcon.Behavior.MinimizeToTray => "minimize",
case TrayIcon.Behavior.MinimizeToTray: return "minimize"; TrayIcon.Behavior.CloseToTray => "close",
case TrayIcon.Behavior.CloseToTray: return "close"; TrayIcon.Behavior.Combined => "combined",
case TrayIcon.Behavior.Combined: return "combined"; _ => "off"
default: return "off"; };
}
}
} }
private static string NotificationPosition{ private static string NotificationPosition{
get{ get => UserConfig.NotificationPosition switch{
switch(UserConfig.NotificationPosition){ TweetNotification.Position.TopLeft => "top left",
case TweetNotification.Position.TopLeft: return "top left"; TweetNotification.Position.TopRight => "top right",
case TweetNotification.Position.TopRight: return "top right"; TweetNotification.Position.BottomLeft => "bottom left",
case TweetNotification.Position.BottomLeft: return "bottom left"; TweetNotification.Position.BottomRight => "bottom right",
case TweetNotification.Position.BottomRight: return "bottom right"; _ => "custom"
default: return "custom"; };
}
}
} }
private static string NotificationSize{ private static string NotificationSize{
get{ get => UserConfig.NotificationSize switch{
switch(UserConfig.NotificationSize){ TweetNotification.Size.Auto => "auto",
case TweetNotification.Size.Auto: return "auto"; _ => RoundUp(UserConfig.CustomNotificationSize.Width, 20) + "x" + RoundUp(UserConfig.CustomNotificationSize.Height, 20)
default: return RoundUp(UserConfig.CustomNotificationSize.Width, 20)+"x"+RoundUp(UserConfig.CustomNotificationSize.Height, 20); };
}
}
} }
private static string NotificationTimer{ private static string NotificationTimer{

View File

@ -64,11 +64,11 @@ private void panelDescription_Resize(object sender, EventArgs e){
int requiredLines = Math.Max(descriptionLines, 1+(string.IsNullOrEmpty(labelVersion.Text) ? 0 : 1)+(isConfigurable ? 1 : 0)); int requiredLines = Math.Max(descriptionLines, 1+(string.IsNullOrEmpty(labelVersion.Text) ? 0 : 1)+(isConfigurable ? 1 : 0));
switch(requiredLines){ nextHeight = requiredLines switch{
case 1: nextHeight = MaximumSize.Height-2*(font.Height-1); break; 1 => MaximumSize.Height - 2 * (font.Height - 1),
case 2: nextHeight = MaximumSize.Height-(font.Height-1); break; 2 => MaximumSize.Height - 1 * (font.Height - 1),
default: nextHeight = MaximumSize.Height; break; _ => MaximumSize.Height
} };
if (nextHeight != Height){ if (nextHeight != Height){
timerLayout.Start(); timerLayout.Start();

View File

@ -25,19 +25,19 @@ public static bool IncludesDisabledPlugins(this PluginEnvironment environment){
} }
public static string? GetPluginScriptFile(this PluginEnvironment environment){ public static string? GetPluginScriptFile(this PluginEnvironment environment){
switch(environment){ return environment switch{
case PluginEnvironment.Browser: return "browser.js"; PluginEnvironment.Browser => "browser.js",
case PluginEnvironment.Notification: return "notification.js"; PluginEnvironment.Notification => "notification.js",
default: return null; _ => null
} };
} }
public static string GetPluginScriptVariables(this PluginEnvironment environment){ public static string GetPluginScriptVariables(this PluginEnvironment environment){
switch(environment){ return environment switch{
case PluginEnvironment.Browser: return "$,$TD,$TDP,TD"; PluginEnvironment.Browser => "$,$TD,$TDP,TD",
case PluginEnvironment.Notification: return "$TD,$TDP"; PluginEnvironment.Notification => "$TD,$TDP",
default: return string.Empty; _ => string.Empty
} };
} }
public static IReadOnlyDictionary<PluginEnvironment, T> Map<T>(T forNone, T forBrowser, T forNotification){ public static IReadOnlyDictionary<PluginEnvironment, T> Map<T>(T forNone, T forBrowser, T forNotification){

View File

@ -5,19 +5,19 @@ public enum PluginGroup{
public static class PluginGroupExtensions{ public static class PluginGroupExtensions{
public static string GetIdentifierPrefix(this PluginGroup group){ public static string GetIdentifierPrefix(this PluginGroup group){
switch(group){ return group switch{
case PluginGroup.Official: return "official/"; PluginGroup.Official => "official/",
case PluginGroup.Custom: return "custom/"; PluginGroup.Custom => "custom/",
default: return "unknown/"; _ => "unknown/"
} };
} }
public static string GetIdentifierPrefixShort(this PluginGroup group){ public static string GetIdentifierPrefixShort(this PluginGroup group){
switch(group){ return group switch{
case PluginGroup.Official: return "o/"; PluginGroup.Official => "o/",
case PluginGroup.Custom: return "c/"; PluginGroup.Custom => "c/",
default: return "?/"; _ => "?/"
} };
} }
} }
} }

View File

@ -71,11 +71,11 @@ public string GetScriptPath(PluginEnvironment environment){
} }
public string GetPluginFolder(PluginFolder folder){ public string GetPluginFolder(PluginFolder folder){
switch(folder){ return folder switch{
case PluginFolder.Root: return pathRoot; PluginFolder.Root => pathRoot,
case PluginFolder.Data: return pathData; PluginFolder.Data => pathData,
default: return string.Empty; _ => string.Empty
} };
} }
public string GetFullPathIfSafe(PluginFolder folder, string relativePath){ public string GetFullPathIfSafe(PluginFolder folder, string relativePath){