Skip to content

Commit

Permalink
多重起動を禁止。設定画面のツールヒントを改善
Browse files Browse the repository at this point in the history
  • Loading branch information
fog-bank committed Jun 9, 2018
1 parent d179d69 commit d8619bb
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 40 deletions.
2 changes: 1 addition & 1 deletion mojp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
x:Class="Mojp.App"
StartupUri="MainWindow.xaml">
<Application.Resources>
<BooleanToVisibilityConverter x:Key="bool2visible" />
</Application.Resources>
</Application>
44 changes: 44 additions & 0 deletions mojp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
Expand Down Expand Up @@ -37,6 +38,26 @@ public partial class App : Application
/// </summary>
public static Dispatcher CurrentDispatcher => Current.Dispatcher;

/// <summary>
/// Application Entry Point.
/// </summary>
[STAThread]
public static void Main()
{
const string AppGuid = "{B99B5E4E-C7AA-4D4C-8674-DBAC723D29D5}";

// 多重起動防止
using (var sema = new Semaphore(0, 1, AppGuid, out bool createdNew))
{
if (createdNew)
{
var app = new App();
app.InitializeComponent();
app.Run();
}
}
}

/// <summary>
/// カードテキストデータを XML に保存します。
/// </summary>
Expand Down Expand Up @@ -114,6 +135,29 @@ public static async Task<bool> IsOutdatedRelease(bool acceptsPrerelease)
return Version.TryParse(version, out var latest) && current < latest;
}

/// <summary>
/// 指定した名前のプロセスを探し、そのプロセス ID を取得します。
/// </summary>
/// <param name="processName">プロセスの名前。</param>
/// <param name="id">取得したプロセス ID 。</param>
/// <returns>プロセスが見つかった場合は <see langword="true"/> 。</returns>
public static bool GetProcessIDByName(string processName, out int id)
{
foreach (var proc in Process.GetProcesses())
{
using (proc)
{
if (string.Equals(proc.ProcessName, processName, StringComparison.OrdinalIgnoreCase))
{
id = proc.Id;
return true;
}
}
}
id = 0;
return false;
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Expand Down
2 changes: 1 addition & 1 deletion mojp/CardTextProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static string NormalizeName(string name)
/// <summary>
/// アーティスト名に対応する英雄譚のカード名を取得します。
/// </summary>
public static bool GetSagaNameByArtist(string artist, out string cardName)
public static bool GetSagaByArtist(string artist, out string cardName)
{
switch (artist)
{
Expand Down
30 changes: 8 additions & 22 deletions mojp/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public void ReleaseAutomationElement()
}
}

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

/// <summary>
Expand All @@ -433,20 +433,7 @@ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
private void OnCapture(object sender, EventArgs e)
{
// MO のプロセス ID を取得する
int? mtgoProcessID = null;

foreach (var proc in Process.GetProcesses())
{
if (string.Equals(proc.ProcessName, "mtgo", StringComparison.OrdinalIgnoreCase))
{
mtgoProcessID = proc.Id;
proc.Dispose();
break;
}
proc.Dispose();
}

if (!mtgoProcessID.HasValue)
if (!App.GetProcessIDByName("mtgo", out int mtgoProcessID))
{
ReleaseAutomationElement();
SetMessage("起動中のプロセスの中に MO が見つかりません。");
Expand All @@ -459,7 +446,7 @@ private void OnCapture(object sender, EventArgs e)
{
currentPrevWnd = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ProcessIdProperty, mtgoProcessID.Value),
new PropertyCondition(AutomationElement.ProcessIdProperty, mtgoProcessID),
new PropertyCondition(AutomationElement.ClassNameProperty, "Window"),
new PropertyCondition(AutomationElement.NameProperty, "Preview")));
}
Expand Down Expand Up @@ -532,10 +519,10 @@ private void OnAutomaionNamePropertyChanged(object sender, AutomationPropertyCha
return;
}
}
else if (Card.GetSagaNameByArtist(name, out string saga))
else if (Card.GetSagaByArtist(name, out string saga))
{
// 英雄譚はカード名を Automation で探せないため、アーティスト名で 1:1 対応で探す
if (CheckCardTypeForSaga(saga))
if (CheckAndViewSaga(saga))
return;
}

Expand Down Expand Up @@ -574,9 +561,9 @@ private void SearchCardName()
return;

// 英雄譚の絵師かもしれない場合
if (Card.GetSagaNameByArtist(name, out string saga))
if (Card.GetSagaByArtist(name, out string saga))
{
if (CheckCardTypeForSaga(saga))
if (CheckAndViewSaga(saga))
return;
}

Expand Down Expand Up @@ -647,7 +634,7 @@ private void SearchCardName()
/// <summary>
/// 現在のカードのカードタイプが英雄譚であるかどうかをチェックし、そうであるなら、指定したカード名のカードを表示します。
/// </summary>
private bool CheckCardTypeForSaga(string cardName)
private bool CheckAndViewSaga(string cardName)
{
if (App.Cards.TryGetValue(cardName, out var foundCard))
{
Expand All @@ -658,7 +645,6 @@ private bool CheckCardTypeForSaga(string cardName)
element = prevWnd?.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, "CardType"));
}

string cardType = GetNamePropertyValue(element);

if (cardType != null && cardType.EndsWith("Saga"))
Expand Down
7 changes: 2 additions & 5 deletions mojp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
Left="{Binding WindowLeft, Mode=TwoWay}" Top="{Binding WindowTop, Mode=TwoWay}"
Icon="Resources/dictionary.ico" SnapsToDevicePixels="True" Initialized="OnInitialized">
<!-- WindowStyle="None" AllowsTransparency="True" Background="Transparent" -->
<Window.Resources>
<BooleanToVisibilityConverter x:Key="bool2vis" />
</Window.Resources>
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
Expand Down Expand Up @@ -142,11 +139,11 @@
</Style>
</TextBlock.Style>
</TextBlock>
<Border Grid.Column="1" Visibility="{Binding DataContext.GetPDList, ElementName=tabControl, Converter={StaticResource bool2vis}}">
<Border Grid.Column="1" Visibility="{Binding DataContext.GetPDList, ElementName=tabControl, Converter={StaticResource bool2visible}}">
<Image Source="Resources/pd.png" Height="18" Margin="6,0,0,0" VerticalAlignment="Center"
Visibility="Collapsed" local:CardPrice.LegalTarget="{Binding}" ToolTip="Penny Dreadful リーガル" />
</Border>
<Border Grid.Column="2" Visibility="{Binding DataContext.GetCardPrice, ElementName=tabControl, Converter={StaticResource bool2vis}}">
<Border Grid.Column="2" Visibility="{Binding DataContext.GetCardPrice, ElementName=tabControl, Converter={StaticResource bool2visible}}">
<TextBlock Margin="6,0,0,0" VerticalAlignment="Center" Text="{Binding Price}"
local:CardPrice.PriceTarget="{Binding}" ToolTip="Scryfall から取得した日毎の平均価格">
<TextBlock.Style>
Expand Down
27 changes: 19 additions & 8 deletions mojp/OptionDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<Style TargetType="CheckBox">
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<BooleanToVisibilityConverter x:Key="bool2visible" />
</Window.Resources>
<Grid>
<StackPanel Margin="4">
Expand Down Expand Up @@ -54,7 +53,13 @@
<!--<TextBlock Margin="2,0,2,4"></TextBlock>-->
</StackPanel>
<CheckBox Grid.Row="2" Grid.ColumnSpan="4" Margin="0,4,0,0" IsChecked="{Binding AutoVersionCheck}"
ToolTip="起動時とこのダイアログを閉じた時に更新があるかどうかを確認します。" ToolTipService.ShowDuration="60000">
ToolTipService.ShowDuration="60000">
<CheckBox.ToolTip>
<TextBlock Margin="0">
起動時とこのダイアログを閉じた時に更新があるかどうかを確認します。<LineBreak />
更新があったときは、ダウンロードサイトへのリンクを表示します。
</TextBlock>
</CheckBox.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock>最新バージョンを自動的に確認する</TextBlock>
<Image Source="Resources/Information.png" Height="16" Margin="5,0,0,0" />
Expand Down Expand Up @@ -114,13 +119,13 @@
<TextBlock Grid.Column="1" Text="{Binding FontSize}" />
</Grid>
<StackPanel Grid.Row="2" Orientation="Horizontal"
ToolTip="カード名の表示言語を指定します。テキスト本文中は《日本語》で固定です。">
<TextBlock>タブに表示するカード名</TextBlock>
ToolTip="タブに表示されるカード名の書式を指定します。ただしテキスト本文中は《日本語》で固定です。" ToolTipService.ShowDuration="60000">
<TextBlock>カード名の表示方法</TextBlock>
<Image Grid.Column="1" Source="Resources/Information.png" Height="16" Margin="5,0" />
</StackPanel>
<ComboBox Name="cmbCardDisplayName" Grid.Row="2" Grid.Column="2" Margin="2" Padding="4"
SelectionChanged="OnCardDisplayNameChanged"
ToolTip="カード名の表示言語を指定します。テキスト本文中は《日本語》で固定です。">
ToolTip="タブに表示されるカード名の書式を指定します。ただしテキスト本文中は《日本語》で固定です。" ToolTipService.ShowDuration="60000">
<ComboBoxItem Padding="2">日本語</ComboBoxItem>
<ComboBoxItem Padding="2">日本語 / English</ComboBoxItem>
<ComboBoxItem Padding="2">English / 日本語</ComboBoxItem>
Expand All @@ -146,14 +151,20 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<CheckBox IsChecked="{Binding ShowBasicLands}"
ToolTip="チェックを外すと、氷雪土地でない平地、島、沼、山、森には反応しなくなります。" ToolTipService.ShowDuration="60000">
ToolTip="チェックを外すと、氷雪でない基本の平地、島、沼、山、森には反応しなくなります。" ToolTipService.ShowDuration="60000">
<StackPanel Orientation="Horizontal">
<TextBlock>基本土地 5 種のカードテキストを表示する</TextBlock>
<Image Source="Resources/Information.png" Height="16" Margin="5,0,0,0" />
</StackPanel>
</CheckBox>
<CheckBox Grid.Row="1" IsChecked="{Binding AutoRefresh}"
ToolTip="チェックを外した場合は「MO を探す」ボタンを適時クリックしてください。" ToolTipService.ShowDuration="60000">
<CheckBox Grid.Row="1" IsChecked="{Binding AutoRefresh}" ToolTipService.ShowDuration="60000">
<CheckBox.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0">チェックを外した場合は「MO を探す」ボタン</TextBlock>
<Image Source="Resources\Camera.png" Height="16" Margin="4,0" />
<TextBlock Margin="0">を適時クリックしてください。</TextBlock>
</StackPanel>
</CheckBox.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock>MO の Preview Pane を自動的に探す</TextBlock>
<Image Source="Resources/Information.png" Height="16" Margin="5,0,0,0" />
Expand Down
2 changes: 1 addition & 1 deletion mojp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.0.0")]
[assembly: AssemblyFileVersion("1.8.30609.19")]
[assembly: AssemblyFileVersion("1.8.30609.20")]
4 changes: 2 additions & 2 deletions mojp/mojp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
</Page>
<Compile Include="SettingsCache.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down

0 comments on commit d8619bb

Please sign in to comment.