-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
159 lines (130 loc) · 4.54 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using log4net;
using SteamGameNotes.Controls;
using SteamGameNotes.DTO;
using SteamGameNotes.Helper;
using SteamGameNotes.Service;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace SteamGameNotes
{
public partial class MainWindow : BaseWindow
{
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
private Brush _txtForeground = null;
private GamesService _gamesService = new GamesService();
private long _steamActiveGameId = 0L;
public MainWindow()
{
_initialize();
}
private void _initialize()
{
InitializeComponent();
_txtForeground = TxtSearchGame.Foreground;
// Placeholder
TxtSearchGame.Foreground = (SolidColorBrush)FindResource("ColorIcons");
TxtSearchGame.Text = TxtSearchGame.Tag.ToString();
try
{
_steamActiveGameId = SteamHelper.GetActiveGameAppId();
}
catch (Exception ex)
{
log.Error("Error getting game appId. ", ex);
}
}
private GameItem _buildGameItem(SteamAppDto game)
{
var contextMenu = new ContextMenu();
var menuItem = new MenuItem();
menuItem.Header = "Remove game";
menuItem.Click += OnDeleteGame;
contextMenu.Items.Add(menuItem);
GameItem item = new GameItem();
item.GameName = game.name;
item.AppId = game.appid;
item.IsPlaying = game.appid.Equals(_steamActiveGameId);
item.ContextMenu = contextMenu;
item.MouseLeftButtonUp += OnGameItemClick;
return item;
}
private async void OnDeleteGame(object sender, RoutedEventArgs e)
{
var menuItem = (MenuItem)sender;
var contextMenu = (ContextMenu)menuItem.Parent;
var gameItem = (GameItem)contextMenu.PlacementTarget;
await _gamesService.Delete(gameItem.AppId);
await _refreshGameList();
}
private async Task _refreshGameList()
{
var games = await _gamesService.ListGames();
PnlGameList.Children.Clear();
foreach (var game in games)
{
PnlGameList.Children.Add(_buildGameItem(game));
}
}
private void OnGameItemClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var gameItem = (GameItem)sender;
Notes notes = new Notes(new SteamAppDto(gameItem.AppId, gameItem.GameName));
notes.Left = this.Left;
notes.Top = this.Top;
notes.Show();
Close();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var steamService = new SteamService();
var searchText = TxtSearchGame.Text;
SteamAppDto game = null;
try
{
var appId = long.Parse(searchText);
game = await steamService.GetSteamAppByAppId(appId);
}
catch
{
game = await steamService.GetSteamAppByName(searchText);
}
if(game != null)
{
try
{
await _gamesService.Create(game);
PnlGameList.Children.Add(_buildGameItem(game));
}
catch(Exception ex)
{
log.Error("Error adding game. ", ex);
}
}
}
private void TxtSearchGame_LostFocus(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(TxtSearchGame.Text))
{
TxtSearchGame.Text = TxtSearchGame.Tag.ToString();
TxtSearchGame.Foreground = (SolidColorBrush)FindResource("ColorIcons");
}
}
private void TxtSearchGame_GotFocus(object sender, RoutedEventArgs e)
{
base.RequestActivateWindow();
TxtSearchGame.Foreground = _txtForeground;
if (TxtSearchGame.Text.Equals(TxtSearchGame.Tag))
{
TxtSearchGame.Text = null;
}
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
base.RequestDeactivateWindow();
await _refreshGameList();
}
}
}