OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / TabsDialog.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Data;
28 using System.Drawing;
29 using System.Linq;
30 using System.Text;
31 using System.Windows.Forms;
32 using OpenTween.Models;
33
34 namespace OpenTween
35 {
36     public partial class TabsDialog : OTBaseForm
37     {
38         private readonly TabInformations TabInfo;
39
40         private bool _MultiSelect = false;
41         public bool MultiSelect
42         {
43             get => this._MultiSelect;
44             set { this._MultiSelect = value; this.UpdateTabList(); }
45         }
46
47         protected internal class TabListItem
48         {
49             public TabModel? Tab { get; set; }
50             public string Label { get; set; } = "";
51
52             public override string ToString()
53                 => this.Label;
54         }
55
56         public TabsDialog(TabInformations tabinformation)
57         {
58             InitializeComponent();
59
60             this.TabInfo = tabinformation;
61             UpdateTabList();
62         }
63
64         protected void UpdateTabList()
65         {
66             this.TabList.Items.Clear();
67
68             if (this.MultiSelect)
69             {
70                 this.TabList.SelectionMode = SelectionMode.MultiExtended;
71             }
72             else
73             {
74                 this.TabList.SelectionMode = SelectionMode.One;
75
76                 this.TabList.Items.Add(new TabListItem
77                 {
78                     Label = Properties.Resources.AddNewTabText1,
79                     Tab = null,
80                 });
81             }
82
83             foreach (var tab in this.TabInfo.Tabs)
84             {
85                 if (!tab.IsDistributableTabType) continue;
86
87                 this.TabList.Items.Add(new TabListItem
88                 {
89                     Label = tab.TabName,
90                     Tab = tab,
91                 });
92             }
93         }
94
95         private void TabList_DoubleClick(object sender, EventArgs e)
96         {
97             if (this.TabList.SelectedIndex == -1) return;
98
99             this.DialogResult = DialogResult.OK;
100             this.Close();
101         }
102
103         private void TabList_SelectedValueChanged(object sender, EventArgs e)
104         {
105             if (this.TabList.SelectedIndex == -1)
106                 this.OK_Button.Enabled = false;
107             else
108                 this.OK_Button.Enabled = true;
109         }
110
111         public TabModel? SelectedTab
112             => this.TabList.SelectedItem is TabListItem item ? item.Tab : null;
113
114         public TabModel[] SelectedTabs
115             => this.TabList.SelectedItems
116                     .Cast<TabListItem>()
117                     .Select(x => x.Tab)
118                     .OfType<TabModel>()
119                     .ToArray();
120     }
121 }