Skip to content

Commit

Permalink
Merge pull request #359 from opentween/disable-secondary-account
Browse files Browse the repository at this point in the history
設定画面でアカウントの有効・無効を切り替える機能を追加
  • Loading branch information
upsilon authored Jun 10, 2024
2 parents 2e888d6 + 4239ace commit e262c25
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 2 deletions.
20 changes: 20 additions & 0 deletions OpenTween.Tests/SocialProtocol/AccountCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ public void SecondaryAccounts_Test()
Assert.Equal(new("00000000-0000-4000-8000-111111111111"), secondaryAccounts[0].UniqueKey);
}

[Fact]
public void SecondaryAccounts_DisabledTest()
{
var disabledAccountSetting = this.CreateAccountSetting("00000000-0000-4000-8000-111111111111");
disabledAccountSetting.Disabled = true;

using var accounts = new AccountCollection();
accounts.LoadFromSettings(new()
{
UserAccounts = new()
{
this.CreateAccountSetting("00000000-0000-4000-8000-000000000000"),
disabledAccountSetting,
},
SelectedAccountKey = new("00000000-0000-4000-8000-000000000000"),
});

Assert.Empty(accounts.SecondaryAccounts);
}

[Fact]
public void GetAccountForTab_DefaultTest()
{
Expand Down
9 changes: 9 additions & 0 deletions OpenTween/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions OpenTween/Properties/Resources.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>

<data name="AccountListBoxItem_Disabled"><value>(Disabled)</value></data>
<data name="AccountListBoxItem_Primary"><value>(Primary)</value></data>
<data name="AccountTypeErrorText"><value>This feature is not available for current account.</value></data>
<data name="AddNewTab_ListView_AccessibleName"><value>PostsList</value></data>
Expand Down
1 change: 1 addition & 0 deletions OpenTween/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

<data name="AccountListBoxItem_Disabled"><value>(無効)</value></data>
<data name="AccountListBoxItem_Primary"><value>(メイン)</value></data>
<data name="AccountTypeErrorText"><value>この機能は現在のアカウントでは使用できません</value></data>
<data name="AddNewTab_ListView_AccessibleName"><value>発言一覧</value></data>
Expand Down
11 changes: 11 additions & 0 deletions OpenTween/Setting/Panel/BasedPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 48 additions & 2 deletions OpenTween/Setting/Panel/BasedPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ private string ComposeDisplayText()
_ => "Twitter / unknown",
};
var accountName = $"@{this.AccountSettings.Username}";
var suffix = this.IsPrimary ? Properties.Resources.AccountListBoxItem_Primary : "";
var suffix = "";
if (this.IsPrimary)
suffix += " " + Properties.Resources.AccountListBoxItem_Primary;
if (this.AccountSettings.Disabled)
suffix += " " + Properties.Resources.AccountListBoxItem_Disabled;

return $"[{authTypeText}] {accountName} {suffix}";
return $"[{authTypeText}] {accountName}{suffix}";
}
}

Expand Down Expand Up @@ -177,10 +181,42 @@ private void MakeAccountPrimaryAt(int index)
this.AccountsList[oldPrimaryIndex] with { IsPrimary = false };
}

// Disabled になっていたら強制的に解除する
this.AccountsList[index].AccountSettings.Disabled = false;

this.AccountsList[index] =
this.AccountsList[index] with { IsPrimary = true };
}

private void ToggleAccountDisabledAt(int index)
{
var listItem = this.AccountsList[index];

// Primary だった場合は無効にしない
if (listItem.IsPrimary)
return;

var accountSettings = listItem.AccountSettings;
accountSettings.Disabled = !accountSettings.Disabled;

this.AccountsList.ResetItem(index);
}

private void UpdateToggleDisabledButton()
{
var selectedIndex = this.AccountsListBox.SelectedIndex;
if (selectedIndex == -1)
return;

var selectedItem = this.AccountsList[selectedIndex];
this.ToggleDisabledButton.Text = selectedItem.AccountSettings.Disabled
? Properties.Resources.EnableButtonCaption
: Properties.Resources.DisableButtonCaption;
}

private void AccountsListBox_SelectedIndexChanged(object sender, EventArgs e)
=> this.UpdateToggleDisabledButton();

private void AddAccountButton_Click(object sender, EventArgs e)
{
this.contextMenuAddAccount.Show(
Expand Down Expand Up @@ -228,5 +264,15 @@ private void MakePrimaryButton_Click(object sender, EventArgs e)
using (ControlTransaction.Update(this.AccountsListBox))
this.MakeAccountPrimaryAt(selectedIndex);
}

private void ToggleDisabledButton_Click(object sender, EventArgs e)
{
var selectedIndex = this.AccountsListBox.SelectedIndex;
if (selectedIndex == -1)
return;

using (ControlTransaction.Update(this.AccountsListBox))
this.ToggleAccountDisabledAt(selectedIndex);
}
}
}
11 changes: 11 additions & 0 deletions OpenTween/Setting/Panel/BasedPanel.resx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<data name="&gt;&gt;RemoveAccountButton.Parent"><value>panel1</value></data>
<data name="&gt;&gt;RemoveAccountButton.Type"><value>System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></data>
<data name="&gt;&gt;RemoveAccountButton.ZOrder"><value>3</value></data>
<data name="&gt;&gt;ToggleDisabledButton.Name"><value>ToggleDisabledButton</value></data>
<data name="&gt;&gt;ToggleDisabledButton.Parent"><value>panel1</value></data>
<data name="&gt;&gt;ToggleDisabledButton.Type"><value>System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></data>
<data name="&gt;&gt;ToggleDisabledButton.ZOrder"><value>5</value></data>
<data name="AccountListLabel.AutoSize" type="System.Boolean, mscorlib"><value>True</value></data>
<data name="AccountListLabel.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms"><value>NoControl</value></data>
<data name="AccountListLabel.Location" type="System.Drawing.Point, System.Drawing"><value>21, 20</value></data>
Expand Down Expand Up @@ -80,4 +84,11 @@
<data name="RemoveAccountButton.Size" type="System.Drawing.Size, System.Drawing"><value>100, 25</value></data>
<data name="RemoveAccountButton.TabIndex" type="System.Int32, mscorlib"><value>3</value></data>
<data name="RemoveAccountButton.Text"><value>削除</value></data>
<data name="ToggleDisabledButton.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms"><value>Top, Right</value></data>
<data name="ToggleDisabledButton.AutoSize" type="System.Boolean, mscorlib"><value>True</value></data>
<data name="ToggleDisabledButton.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms"><value>NoControl</value></data>
<data name="ToggleDisabledButton.Location" type="System.Drawing.Point, System.Drawing"><value>396, 135</value></data>
<data name="ToggleDisabledButton.Size" type="System.Drawing.Size, System.Drawing"><value>100, 25</value></data>
<data name="ToggleDisabledButton.TabIndex" type="System.Int32, mscorlib"><value>5</value></data>
<data name="ToggleDisabledButton.Text"><value>無効化</value></data>
</root>
3 changes: 3 additions & 0 deletions OpenTween/Setting/SettingCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ public class UserAccount
{
public Guid UniqueKey { get; set; } = Guid.NewGuid();

[DefaultValue(false)]
public bool Disabled { get; set; }

[DefaultValue("Twitter")]
public string AccountType { get; set; } = "Twitter";

Expand Down
3 changes: 3 additions & 0 deletions OpenTween/SocialProtocol/AccountCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public void LoadFromSettings(SettingCommon settingCommon)

foreach (var accountSettings in settingCommon.UserAccounts)
{
if (accountSettings.Disabled)
continue;

var accountKey = accountSettings.UniqueKey;

if (oldAccounts.TryGetValue(accountKey, out var account))
Expand Down
14 changes: 14 additions & 0 deletions OpenTween/Tween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,7 @@ private async void SettingStripMenuItem_Click(object sender, EventArgs e)
var newSecondaryAccounts = currentSecondaryAccounts
.Where(x => !previousSecondaryAccounts.Any(y => y.UniqueKey == x.UniqueKey));
this.AddSecondaryAccountTabs(newSecondaryAccounts);
this.RemoveMissingAccountTabs();
}

private void AddSecondaryAccountTabs(IEnumerable<ISocialAccount> accounts)
Expand All @@ -2612,6 +2613,19 @@ private void AddSecondaryAccountTabs(IEnumerable<ISocialAccount> accounts)
}
}

private void RemoveMissingAccountTabs()
{
var secondaryAccounts = this.accounts.SecondaryAccounts;
var secondaryAccountTabs = this.statuses.GetTabsByType<HomeSpecifiedAccountTabModel>();

foreach (var tab in secondaryAccountTabs)
{
var isAccountExists = secondaryAccounts.Any(x => x.UniqueKey == tab.SourceAccountId);
if (!isAccountExists)
this.RemoveSpecifiedTab(tab.TabName, confirm: false);
}
}

/// <summary>
/// タブの表示位置を設定する
/// </summary>
Expand Down

0 comments on commit e262c25

Please sign in to comment.