1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-08 02: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){
MessageBoxIcon icon;
switch(type){
case "error": icon = MessageBoxIcon.Error; break;
case "warning": icon = MessageBoxIcon.Warning; break;
case "info": icon = MessageBoxIcon.Information; break;
default: icon = MessageBoxIcon.None; break;
}
MessageBoxIcon icon = type switch{
"error" => MessageBoxIcon.Error,
"warning" => MessageBoxIcon.Warning,
"info" => MessageBoxIcon.Information,
_ => MessageBoxIcon.None
};
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('|');
if (pipe != -1){
switch(text.Substring(0, pipe)){
case "error": icon = MessageBoxIcon.Error; break;
case "warning": icon = MessageBoxIcon.Warning; break;
case "info": icon = MessageBoxIcon.Information; break;
case "question": icon = MessageBoxIcon.Question; break;
default: return new FormMessage(caption, text, icon);
}
icon = text.Substring(0, pipe) switch{
"error" => MessageBoxIcon.Error,
"warning" => MessageBoxIcon.Warning,
"info" => MessageBoxIcon.Information,
"question" => MessageBoxIcon.Question,
_ => MessageBoxIcon.None
};
text = text.Substring(pipe+1);
if (icon != MessageBoxIcon.None){
text = text.Substring(pipe + 1);
}
}
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 int FontSizeLevel{
get{
switch(TweetDeckBridge.FontSize){
case "largest": return 4;
case "large": return 3;
case "small": return 1;
case "smallest": return 0;
default: return 2;
}
}
get => TweetDeckBridge.FontSize switch{
"largest" => 4,
"large" => 3,
"small" => 1,
"smallest" => 0,
_ => 2
};
}
protected virtual Point PrimaryLocation{

View File

@ -44,27 +44,17 @@ public virtual bool RequiresResize{
}
private int BaseClientWidth{
get{
switch(Config.NotificationSize){
default:
return BrowserUtils.Scale(284, SizeScale*(1.0+0.05*FontSizeLevel));
case TweetNotification.Size.Custom:
return Config.CustomNotificationSize.Width;
}
}
get => Config.NotificationSize switch{
TweetNotification.Size.Custom => Config.CustomNotificationSize.Width,
_ => BrowserUtils.Scale(284, SizeScale * (1.0 + 0.05 * FontSizeLevel))
};
}
private int BaseClientHeight{
get{
switch(Config.NotificationSize){
default:
return BrowserUtils.Scale(122, SizeScale*(1.0+0.08*FontSizeLevel));
case TweetNotification.Size.Custom:
return Config.CustomNotificationSize.Height;
}
}
get => Config.NotificationSize switch{
TweetNotification.Size.Custom => Config.CustomNotificationSize.Height,
_ => BrowserUtils.Scale(122, SizeScale * (1.0 + 0.08 * FontSizeLevel))
};
}
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 static IResourceHandler CreateFileHandler(string path){
string mimeType;
switch(Path.GetExtension(path)){
case ".weba":
case ".webm": mimeType = "audio/webm"; break;
case ".wav": mimeType = "audio/wav"; break;
case ".ogg": mimeType = "audio/ogg"; break;
case ".mp3": mimeType = "audio/mp3"; break;
case ".flac": mimeType = "audio/flac"; break;
case ".opus": mimeType = "audio/ogg; codecs=opus"; break;
default: mimeType = null; break;
}
string mimeType = Path.GetExtension(path) switch{
".weba" => "audio/webm",
".webm" => "audio/webm",
".wav" => "audio/wav",
".ogg" => "audio/ogg",
".mp3" => "audio/mp3",
".flac" => "audio/flac",
".opus" => "audio/ogg; codecs=opus",
_ => null
};
try{
return ResourceHandler.FromFilePath(path, mimeType);

View File

@ -207,36 +207,30 @@ private static string CustomBrowser{
}
private static string TrayMode{
get{
switch(UserConfig.TrayBehavior){
case TrayIcon.Behavior.DisplayOnly: return "icon";
case TrayIcon.Behavior.MinimizeToTray: return "minimize";
case TrayIcon.Behavior.CloseToTray: return "close";
case TrayIcon.Behavior.Combined: return "combined";
default: return "off";
}
}
get => UserConfig.TrayBehavior switch{
TrayIcon.Behavior.DisplayOnly => "icon",
TrayIcon.Behavior.MinimizeToTray => "minimize",
TrayIcon.Behavior.CloseToTray => "close",
TrayIcon.Behavior.Combined => "combined",
_ => "off"
};
}
private static string NotificationPosition{
get{
switch(UserConfig.NotificationPosition){
case TweetNotification.Position.TopLeft: return "top left";
case TweetNotification.Position.TopRight: return "top right";
case TweetNotification.Position.BottomLeft: return "bottom left";
case TweetNotification.Position.BottomRight: return "bottom right";
default: return "custom";
}
}
get => UserConfig.NotificationPosition switch{
TweetNotification.Position.TopLeft => "top left",
TweetNotification.Position.TopRight => "top right",
TweetNotification.Position.BottomLeft => "bottom left",
TweetNotification.Position.BottomRight => "bottom right",
_ => "custom"
};
}
private static string NotificationSize{
get{
switch(UserConfig.NotificationSize){
case TweetNotification.Size.Auto: return "auto";
default: return RoundUp(UserConfig.CustomNotificationSize.Width, 20)+"x"+RoundUp(UserConfig.CustomNotificationSize.Height, 20);
}
}
get => UserConfig.NotificationSize switch{
TweetNotification.Size.Auto => "auto",
_ => RoundUp(UserConfig.CustomNotificationSize.Width, 20) + "x" + RoundUp(UserConfig.CustomNotificationSize.Height, 20)
};
}
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));
switch(requiredLines){
case 1: nextHeight = MaximumSize.Height-2*(font.Height-1); break;
case 2: nextHeight = MaximumSize.Height-(font.Height-1); break;
default: nextHeight = MaximumSize.Height; break;
}
nextHeight = requiredLines switch{
1 => MaximumSize.Height - 2 * (font.Height - 1),
2 => MaximumSize.Height - 1 * (font.Height - 1),
_ => MaximumSize.Height
};
if (nextHeight != Height){
timerLayout.Start();

View File

@ -25,19 +25,19 @@ public static bool IncludesDisabledPlugins(this PluginEnvironment environment){
}
public static string? GetPluginScriptFile(this PluginEnvironment environment){
switch(environment){
case PluginEnvironment.Browser: return "browser.js";
case PluginEnvironment.Notification: return "notification.js";
default: return null;
}
return environment switch{
PluginEnvironment.Browser => "browser.js",
PluginEnvironment.Notification => "notification.js",
_ => null
};
}
public static string GetPluginScriptVariables(this PluginEnvironment environment){
switch(environment){
case PluginEnvironment.Browser: return "$,$TD,$TDP,TD";
case PluginEnvironment.Notification: return "$TD,$TDP";
default: return string.Empty;
}
return environment switch{
PluginEnvironment.Browser => "$,$TD,$TDP,TD",
PluginEnvironment.Notification => "$TD,$TDP",
_ => string.Empty
};
}
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 string GetIdentifierPrefix(this PluginGroup group){
switch(group){
case PluginGroup.Official: return "official/";
case PluginGroup.Custom: return "custom/";
default: return "unknown/";
}
return group switch{
PluginGroup.Official => "official/",
PluginGroup.Custom => "custom/",
_ => "unknown/"
};
}
public static string GetIdentifierPrefixShort(this PluginGroup group){
switch(group){
case PluginGroup.Official: return "o/";
case PluginGroup.Custom: return "c/";
default: return "?/";
}
return group switch{
PluginGroup.Official => "o/",
PluginGroup.Custom => "c/",
_ => "?/"
};
}
}
}

View File

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