Skip to content
Snippets Groups Projects
Commit e3d53ce3 authored by Philipp Müller's avatar Philipp Müller
Browse files

added edit button

parent b604ec7b
Branches
No related tags found
1 merge request!28added program list
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
<Compile Update="Screens\Templates\HeaderTemplate.xaml.cs"> <Compile Update="Screens\Templates\HeaderTemplate.xaml.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Update="Utils\InputDialog.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Update="Screens\FirstLaunch\03_2ProgramsList.xaml"> <Page Update="Screens\FirstLaunch\03_2ProgramsList.xaml">
...@@ -48,5 +51,8 @@ ...@@ -48,5 +51,8 @@
<Page Update="Styles\Styles.xaml"> <Page Update="Styles\Styles.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Update="Utils\InputDialog.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<!-- Überschrift --> <!-- Überschrift -->
<TextBlock Grid.Column="0" <TextBlock Grid.Column="0"
Style="{StaticResource Header1}" Style="{StaticResource Header1}"
Text="Edit the list of websites and programs for this category" Text="Edit the list of distracting programs"
HorizontalAlignment="Left" Margin="0,46,0,-46" /> HorizontalAlignment="Left" Margin="0,46,0,-46" />
<!-- Suchfeld --> <!-- Suchfeld -->
...@@ -84,7 +84,7 @@ ...@@ -84,7 +84,7 @@
</DockPanel> </DockPanel>
</Grid> </Grid>
<TextBlock Style="{StaticResource StandardText}" Margin="0,40,0,0" <TextBlock Style="{StaticResource StandardText}" Margin="0,40,0,0"
Text="By clicking on a toggle, you can change the programs/websites that should be marked as distracting." /> Text="By clicking on a toggle, you can change the programs that should be marked as distracting." />
<Border Background="#2C2C2C" CornerRadius="10" Padding="10" Margin="0,10,0,0"> <Border Background="#2C2C2C" CornerRadius="10" Padding="10" Margin="0,10,0,0">
<Grid> <Grid>
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<!-- Label --> <!-- Label -->
<TextBlock Text="Add a distracting website:" <TextBlock Text="Add a distracting program:"
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="White" Foreground="White"
FontSize="14" FontSize="14"
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
BorderThickness="1" BorderThickness="1"
TextChanged="UrlInputBox_TextChanged" /> TextChanged="UrlInputBox_TextChanged" />
<TextBlock x:Name="UrlPlaceholder" <TextBlock x:Name="UrlPlaceholder"
Text="Url" Text="process name"
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="Gray" Foreground="Gray"
FontSize="14" FontSize="14"
...@@ -153,20 +153,20 @@ ...@@ -153,20 +153,20 @@
Style="{StaticResource Header2}" Style="{StaticResource Header2}"
Text="CATEGORY" RenderTransformOrigin="0.548,1.689" Margin="10,0,0,0"/> Text="CATEGORY" RenderTransformOrigin="0.548,1.689" Margin="10,0,0,0"/>
</StackPanel> </StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,10,0,10" Width="350"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,10,0,10" Width="430">
<TextBlock x:Name="HeaderTextBlock2" <TextBlock x:Name="HeaderTextBlock2"
Style="{StaticResource Header2}" Style="{StaticResource Header2}"
Text="Running Programs" RenderTransformOrigin="0.548,1.689" Margin="0,0,0,0" HorizontalAlignment="Right"/> Text="Running Programs" RenderTransformOrigin="0.548,1.689" Margin="0,0,0,0" HorizontalAlignment="Right"/>
<Button Width="110" Margin="70,0,0,0" ToolTip="Refresh" Click="RefreshButton_Click"> <Button Width="110" Margin="150,0,0,0" ToolTip="Refresh" Click="RefreshButton_Click">
<TextBlock Text="Click to Refresh"/> <TextBlock Text="Click to Refresh"/>
</Button> </Button>
</StackPanel> </StackPanel>
</Grid> </Grid>
</StackPanel> </StackPanel>
<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="20,215,20,8" Width="400px" HorizontalAlignment="Left"> <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="20,225,20,8" Width="400px" HorizontalAlignment="Left">
<StackPanel x:Name="ItemsPanel" /> <StackPanel x:Name="ItemsPanel" />
</ScrollViewer> </ScrollViewer>
<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="20,215,20,8" Width="400px" HorizontalAlignment="Right"> <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="20,225,20,8" Width="400px" HorizontalAlignment="Right">
<StackPanel x:Name="ItemsPanel2" /> <StackPanel x:Name="ItemsPanel2" />
</ScrollViewer> </ScrollViewer>
... ...
......
...@@ -17,6 +17,7 @@ using InnoLabProjektDektopApp.Services; ...@@ -17,6 +17,7 @@ using InnoLabProjektDektopApp.Services;
using System.Diagnostics; using System.Diagnostics;
using static InnoLabProjektDektopApp.DistractionsList; using static InnoLabProjektDektopApp.DistractionsList;
using System; using System;
using InnoLabProjektDektopApp.Utils;
namespace InnoLabProjektDektopApp namespace InnoLabProjektDektopApp
...@@ -30,7 +31,8 @@ namespace InnoLabProjektDektopApp ...@@ -30,7 +31,8 @@ namespace InnoLabProjektDektopApp
{ {
InitializeComponent(); InitializeComponent();
_jsonFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "distractingPrograms.json"); _jsonFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", "distractingPrograms.json");
LoadCategoryItems(); LoadProgramList();
RefreshRunningProcessList();
} }
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e) private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
...@@ -101,6 +103,14 @@ namespace InnoLabProjektDektopApp ...@@ -101,6 +103,14 @@ namespace InnoLabProjektDektopApp
_data["Programs"] = new List<ProcessEntry>(); _data["Programs"] = new List<ProcessEntry>();
} }
// Check if the process is already in the list
if (_data["Programs"].Any(p => p.ProcessName.Equals(enteredUrl, StringComparison.CurrentCultureIgnoreCase)))
{
MessageBox.Show($"Process '{enteredUrl}' is already added.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
UrlInputBox.Clear();
return;
}
_data["Programs"].Add(new ProcessEntry _data["Programs"].Add(new ProcessEntry
{ {
ProcessName = enteredUrl, ProcessName = enteredUrl,
...@@ -128,26 +138,29 @@ namespace InnoLabProjektDektopApp ...@@ -128,26 +138,29 @@ namespace InnoLabProjektDektopApp
stackPanel.Children.Add(checkBox); stackPanel.Children.Add(checkBox);
ItemsPanel.Children.Add(stackPanel); ItemsPanel.Children.Add(stackPanel);
MessageBox.Show($"Process '{enteredUrl}' has been added to the 'Programs' category.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
// Clear the input field // Clear the input field
UrlInputBox.Clear(); UrlInputBox.Clear();
// Refresh Panel to exclude duplicates
RefreshRunningProcessList();
} }
private void RefreshButton_Click(object sender, RoutedEventArgs e) private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
RefreshRunningProcessList();
}
private void RefreshRunningProcessList()
{ {
var windowedProcesses = ProcessMonitor.GetWindowedProcesses(); var windowedProcesses = ProcessMonitor.GetWindowedProcesses();
ItemsPanel2.Children.Clear(); ItemsPanel2.Children.Clear();
foreach (var process in windowedProcesses) foreach (var process in windowedProcesses)
{ {
// check if the process is not already in the list // check if the process is not already in the list
if(_data["Programs"].Any(p => p.ProcessName == process.Name.ToLower())) continue; if (_data["Programs"].Any(p => p.ProcessName.Equals(process.Name, StringComparison.CurrentCultureIgnoreCase))) continue;
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 2, 5, 2) }; var stackPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 2, 5, 2) };
// a button with the tag "+"next to the button write the processName: mainWindowTitle
// if a process has been added to the left list (clicked the Add button), delete its entry from the right list
var button = new Button var button = new Button
{ {
Content = "+", Content = "+",
...@@ -157,24 +170,45 @@ namespace InnoLabProjektDektopApp ...@@ -157,24 +170,45 @@ namespace InnoLabProjektDektopApp
var textBlock = new TextBlock var textBlock = new TextBlock
{ {
Text = $"{process.Name}: {process.MainWindowTitle}", Text = $"{process.MainWindowTitle} ({process.Name})",
Margin = new Thickness(5) Margin = new Thickness(5),
ToolTip = $"{process.MainWindowTitle} ({process.Name})"
}; };
button.Click += (s, args) => button.Click += (s, args) =>
{ {
var processName = process.Name; AddProcessFromList(process.Name, process.MainWindowTitle);
var mainWindowTitle = process.MainWindowTitle; };
stackPanel.Children.Add(button);
stackPanel.Children.Add(textBlock);
ItemsPanel2.Children.Add(stackPanel);
}
}
private void AddProcessFromList(string processName, string mainWindowTitle)
{
// Check if the process is already in the list // Check if the process is already in the list
if (_data.ContainsKey("Programs") && _data["Programs"].Any(p => p.ProcessName == processName)) if (_data.TryGetValue("Programs", out List<ProcessEntry>? value) && value.Any(p => p.ProcessName.Equals(processName, StringComparison.CurrentCultureIgnoreCase)))
{ {
MessageBox.Show($"Process '{processName}' is already added.", "Info", MessageBoxButton.OK, MessageBoxImage.Information); MessageBox.Show($"Process '{processName}' is already added.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
return; return;
} }
//show a message box to give the user the option to change the main window title
var inputDialog = new InputDialog("Enter a new Title:", mainWindowTitle); // Assuming InputDialog is a custom dialog
if (inputDialog.ShowDialog() == true)
{
mainWindowTitle = inputDialog.Input; // Get updated title
}
if (inputDialog.DialogResult == false) return;
// Add the process to the list // Add the process to the list
if (!_data.ContainsKey("Programs")) if (!_data.ContainsKey("Programs"))
{ {
_data["Programs"] = new List<ProcessEntry>(); _data["Programs"] = [];
} }
_data["Programs"].Add(new ProcessEntry _data["Programs"].Add(new ProcessEntry
{ {
...@@ -182,27 +216,52 @@ namespace InnoLabProjektDektopApp ...@@ -182,27 +216,52 @@ namespace InnoLabProjektDektopApp
MainWindowTitle = mainWindowTitle, MainWindowTitle = mainWindowTitle,
IsDistracting = true IsDistracting = true
}); });
ShowNewItemInList(processName, mainWindowTitle, true);
RefreshRunningProcessList();
// Save changes to the JSON // Save changes to the JSON
SaveData(); SaveData();
}
private void ShowNewItemInList(string processName, string mainWindowTitle, bool isDistracting)
{
// Add to UI dynamically // Add to UI dynamically
var newStackPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 2, 5, 2) }; var newStackPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 2, 5, 2) };
var newCheckBox = new CheckBox var newCheckBox = new CheckBox
{ {
Content = $"{processName}: {mainWindowTitle}", Content = $"{mainWindowTitle} ({processName})",
Margin = new Thickness(5), Margin = new Thickness(5),
IsChecked = true, IsChecked = true,
ToolTip = $"{mainWindowTitle} ({processName})",
Tag = new ProcessEntry { ProcessName = processName, MainWindowTitle = mainWindowTitle, IsDistracting = true } Tag = new ProcessEntry { ProcessName = processName, MainWindowTitle = mainWindowTitle, IsDistracting = true }
}; };
newCheckBox.Checked += CheckBox_CheckedChanged; newCheckBox.Checked += CheckBox_CheckedChanged;
newCheckBox.Unchecked += CheckBox_CheckedChanged; newCheckBox.Unchecked += CheckBox_CheckedChanged;
newStackPanel.Children.Add(newCheckBox);
ItemsPanel.Children.Add(newStackPanel); var editButton = new Button
{
Content = "Edit",
Margin = new Thickness(5, 0, 0, 0),
Tag = newCheckBox // Store reference to checkbox for easier editing
}; };
editButton.Click += (s, e) => EditItemInList(processName, mainWindowTitle);
stackPanel.Children.Add(button); newStackPanel.Children.Add(newCheckBox);
stackPanel.Children.Add(textBlock); newStackPanel.Children.Add(editButton);
ItemsPanel2.Children.Add(stackPanel); ItemsPanel.Children.Add(newStackPanel);
} }
private void EditItemInList(string processName, string mainWindowTitle)
{
var inputDialog = new InputDialog("Enter a new Title:", mainWindowTitle);
inputDialog.ShowDialog();
if (inputDialog.DialogResult == false || inputDialog.Input.Length == 0) return;
_data["Programs"].FirstOrDefault(p => p.ProcessName.Equals(processName, StringComparison.CurrentCultureIgnoreCase))!.MainWindowTitle = inputDialog.Input;
SaveData();
RefreshProgramList();
} }
...@@ -219,16 +278,19 @@ namespace InnoLabProjektDektopApp ...@@ -219,16 +278,19 @@ namespace InnoLabProjektDektopApp
} }
} }
private void LoadCategoryItems() public void RefreshProgramList()
{
try
{ {
LoadProgramList(); ItemsPanel.Children.Clear();
}
catch (Exception ex) // Kategorie-Elemente laden
var items = _data["Programs"];
foreach (var item in items)
{ {
MessageBox.Show($"Error loading items: {ex.Message}"); ShowNewItemInList(item.ProcessName, item.MainWindowTitle, item.IsDistracting);
} }
// Überschrift setzen
HeaderTextBlock.Text = "Programs";
} }
public void LoadProgramList() public void LoadProgramList()
...@@ -247,24 +309,7 @@ namespace InnoLabProjektDektopApp ...@@ -247,24 +309,7 @@ namespace InnoLabProjektDektopApp
var items = _data["Programs"]; var items = _data["Programs"];
foreach (var item in items) foreach (var item in items)
{ {
// Checkbox erstellen ShowNewItemInList(item.ProcessName, item.MainWindowTitle, item.IsDistracting);
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 2, 5, 2) };
var checkBox = new CheckBox
{
Content = $"{item.ProcessName}: {item.MainWindowTitle}",
Margin = new Thickness(5),
IsChecked = item.IsDistracting,
Tag = item // Speichert die Referenz zum Item
};
checkBox.Checked += CheckBox_CheckedChanged;
checkBox.Unchecked += CheckBox_CheckedChanged;
stackPanel.Children.Add(checkBox);
// Elemente zur Liste hinzufügen
ItemsPanel.Children.Add(stackPanel);
} }
// Überschrift setzen // Überschrift setzen
...@@ -279,12 +324,6 @@ namespace InnoLabProjektDektopApp ...@@ -279,12 +324,6 @@ namespace InnoLabProjektDektopApp
{ {
MessageBox.Show($"Error loading items: {ex.Message}"); MessageBox.Show($"Error loading items: {ex.Message}");
} }
var processes = ProcessMonitor.GetWindowedProcesses();
foreach (var process in processes)
{
Debug.WriteLine($"Process: {process.Name}, Title: {process.MainWindowTitle}");
}
} }
... ...
......
...@@ -76,6 +76,7 @@ namespace InnoLabProjektDektopApp.Services ...@@ -76,6 +76,7 @@ namespace InnoLabProjektDektopApp.Services
{ {
var processes = Process.GetProcesses() var processes = Process.GetProcesses()
.Where(p => !string.IsNullOrWhiteSpace(p.MainWindowTitle)) .Where(p => !string.IsNullOrWhiteSpace(p.MainWindowTitle))
.Where(p => p.ProcessName.ToLower() != "coflow")
.Select(p => (p.ProcessName, p.MainWindowTitle)) .Select(p => (p.ProcessName, p.MainWindowTitle))
.ToList(); .ToList();
return processes; return processes;
... ...
......
<Window x:Class="InnoLabProjektDektopApp.Utils.InputDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Input Dialog" Height="150" Width="300" WindowStartupLocation="CenterOwner">
<StackPanel Margin="10">
<TextBlock x:Name="PromptText" Text="Enter your input:" Margin="0,0,0,10" TextWrapping="Wrap"/>
<TextBox x:Name="InputTextBox" Margin="0,0,0,10"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="OK" Click="OK_Click" Width="75" Margin="0,0,10,0" IsDefault="True"/>
<Button Content="Cancel" Click="Cancel_Click" Width="75" IsCancel="True"/>
</StackPanel>
</StackPanel>
</Window>
using System.Windows;
namespace InnoLabProjektDektopApp.Utils
{
/// <summary>
/// Interaction logic for InputDialog.xaml
/// </summary>
public partial class InputDialog : Window
{
public string Input { get; private set; }
public InputDialog(string prompt, string defaultValue = "")
{
InitializeComponent();
PromptText.Text = prompt; // Ensure XAML has a TextBlock named PromptText
InputTextBox.Text = defaultValue; // Ensure XAML has a TextBox named InputTextBox
InputTextBox.Focus();
}
private void OK_Click(object sender, RoutedEventArgs e)
{
Input = InputTextBox.Text;
DialogResult = true; // Sets DialogResult and allows the window to close
Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false; // Indicates cancellation
Close();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment