Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cerevisiae committed May 29, 2021
0 parents commit 918b211
Show file tree
Hide file tree
Showing 21 changed files with 534 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/*
.vs/*
65 changes: 65 additions & 0 deletions Hitomi.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{05260BE2-EF5A-46FC-8E63-3CE2E236543B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hitomi", "src\Hitomi\Hitomi.csproj", "{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Hitomi.Core", "src\Hitomi.Core\Hitomi.Core.fsproj", "{7EE329FF-F874-40E4-A518-5928FA405BDB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D29B0512-37B7-4880-82F6-6248560F0F6D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Hitomi.Tests", "tests\Hitomi.Tests\Hitomi.Tests.fsproj", "{006A13CD-55FE-4AED-8E97-AE76B4A72717}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|x64.Build.0 = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Debug|x86.Build.0 = Debug|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|Any CPU.Build.0 = Release|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|x64.ActiveCfg = Release|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|x64.Build.0 = Release|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|x86.ActiveCfg = Release|Any CPU
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8}.Release|x86.Build.0 = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|x64.ActiveCfg = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|x64.Build.0 = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|x86.ActiveCfg = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Debug|x86.Build.0 = Debug|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|Any CPU.Build.0 = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|x64.ActiveCfg = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|x64.Build.0 = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|x86.ActiveCfg = Release|Any CPU
{7EE329FF-F874-40E4-A518-5928FA405BDB}.Release|x86.Build.0 = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Debug|x64.ActiveCfg = Debug|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Debug|x64.Build.0 = Debug|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Debug|x86.ActiveCfg = Debug|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Debug|x86.Build.0 = Debug|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|Any CPU.ActiveCfg = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|Any CPU.Build.0 = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|x64.ActiveCfg = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|x64.Build.0 = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|x86.ActiveCfg = Release|Any CPU
{006A13CD-55FE-4AED-8E97-AE76B4A72717}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{35E9828C-E2D6-46D2-BDED-C06A7459BBD8} = {05260BE2-EF5A-46FC-8E63-3CE2E236543B}
{7EE329FF-F874-40E4-A518-5928FA405BDB} = {05260BE2-EF5A-46FC-8E63-3CE2E236543B}
{006A13CD-55FE-4AED-8E97-AE76B4A72717} = {D29B0512-37B7-4880-82F6-6248560F0F6D}
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions Hitomi.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=05687d43_002Dceba_002D403c_002D8c5d_002Df938ce8a61e8/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 s-cerevisiae

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hitomi

Hitomi is a small windows GUI utility which displays the most CPU consuming process in real time.

> Fans shall spin for a reason.
18 changes: 18 additions & 0 deletions src/Hitomi.Core/Hitomi.Core.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

<ItemGroup>
<Compile Include="Process.fs" />
<Compile Include="View.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elmish.WPF" Version="3.5.8" />
</ItemGroup>

</Project>
64 changes: 64 additions & 0 deletions src/Hitomi.Core/Process.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Hitomi.Core.Process

open System
open System.ComponentModel
open System.Diagnostics

let processorCount = Environment.ProcessorCount

let updateInterval = TimeSpan.FromSeconds 1.0

[<Struct>]
type ProcessInterval =
{ Name: string
Interval: TimeSpan }
member this.cpuPercentage(elapsed: TimeSpan) =
float this.Interval.Ticks / float elapsed.Ticks
* 100.0
/ float processorCount
|> int

/// Represents a process and its total cpu time
[<Struct>]
type ProcessorTime =
{ Name: string
ProcessorTime: TimeSpan }
/// Extracts fields from a Process object.
static member extract(p: Process) =
let id = p.Id
let name = p.ProcessName

try
Some(id, { Name = name; ProcessorTime = p.TotalProcessorTime })
with
// Needs admin privilege
| :? Win32Exception
// Process already stopped
| :? InvalidOperationException as e ->
eprintfn $"{id}, {name}, {e.Message}"
None

static member (-)(p1, p0) =
{ Name = p1.Name
Interval = p1.ProcessorTime - p0.ProcessorTime }

type ProcessEntry =
{ Name: string
Percentage: int }
static member ofProcessInterval elapsed (t: ProcessInterval) =
{ Name = t.Name; Percentage = t.cpuPercentage(elapsed) }

/// Records processes and their total cpu time, indexed by process id.
type ProcessMap = Map<int, ProcessorTime>

module ProcessMap =
/// Gets all running processes which are available.
let getAll () =
Process.GetProcesses()
|> Array.choose ProcessorTime.extract
|> Map.ofArray

/// Get a list of time intervals from
let listIntervals (m0: ProcessMap) (m1: ProcessMap) =
m1
|> Seq.choose (fun p -> m0.TryFind p.Key |> Option.map ((-) p.Value))
13 changes: 13 additions & 0 deletions src/Hitomi.Core/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Hitomi.Core.Program

open Elmish
open Elmish.WPF

open Hitomi.Core.View

let designVm = ViewModel.designInstance (init ()) (bindings ())

let main window =
Program.mkSimpleWpf init update bindings
|> Program.withSubscription timer
|> Program.startElmishLoop ElmConfig.Default window
49 changes: 49 additions & 0 deletions src/Hitomi.Core/View.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Hitomi.Core.View

open Hitomi.Core.Process

open System
open System.Diagnostics
open Elmish
open Elmish.WPF

type Model =
{ LastMap: ProcessMap
Watch: Stopwatch
LastTime: TimeSpan
Entry: ProcessEntry }

let init () =
{ LastMap = ProcessMap.getAll ()
Watch = Stopwatch.StartNew()
LastTime = TimeSpan.Zero
Entry = { Name = "null"; Percentage = 0 } }

type Msg = Tick

let update Tick model =
let currentTime = model.Watch.Elapsed
let currentMap = ProcessMap.getAll ()

let entry =
ProcessMap.listIntervals model.LastMap currentMap
|> Seq.maxBy (fun t -> t.Interval)
|> ProcessEntry.ofProcessInterval (currentTime - model.LastTime)

{ model with
LastMap = currentMap
Entry = entry
LastTime = currentTime }


let bindings () : Binding<Model, Msg> list =
[ "ProcessName"
|> Binding.oneWay (fun m -> m.Entry.Name)
"Percentage"
|> Binding.oneWay (fun m -> m.Entry.Percentage) ]

let timer _ =
let timer = new Timers.Timer(float updateInterval.TotalMilliseconds)
timer.Start()
let sub dispatch = timer.Elapsed.Add(fun _ -> dispatch Tick)
Cmd.ofSub sub
11 changes: 11 additions & 0 deletions src/Hitomi/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Application x:Class="Hitomi.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ImageSource x:Key="Icon">pack://application:,,,/hitomi.ico</ImageSource>
<ContextMenu x:Key="Menu">
<MenuItem Header="Exit" Click="Exit_OnClick"/>
</ContextMenu>
</Application.Resources>
</Application>
28 changes: 28 additions & 0 deletions src/Hitomi/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Windows;
using Hitomi.Core;

namespace Hitomi
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
public App()
{
Activated += StartElmish;
}

private void StartElmish(object sender, EventArgs e)
{
Activated -= StartElmish;
Program.main(MainWindow);
}

private void Exit_OnClick(object sender, RoutedEventArgs e)
{
Current.Shutdown();
}
}
}
10 changes: 10 additions & 0 deletions src/Hitomi/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
23 changes: 23 additions & 0 deletions src/Hitomi/Hitomi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<!-- <OutputType>WinExe</OutputType>-->
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<ApplicationIcon>hitomi.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Hitomi.Core\Hitomi.Core.fsproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<Resource Include="hitomi.ico" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/Hitomi/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Window x:Class="Hitomi.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Hitomi"
xmlns:core="clr-namespace:Hitomi.Core;assembly=Hitomi.Core"
mc:Ignorable="d"
Title="Hitomi"
WindowStyle="None" ResizeMode="NoResize"
AllowsTransparency="True"
Background="Transparent"
ShowInTaskbar="False"
SizeToContent="WidthAndHeight"
d:DataContext="{x:Static core:Program.designVm}"
MouseDown="MainWindow_OnMouseDown"
Deactivated="MainWindow_OnDeactivated">
<Grid>
<local:NotifyIcon />
<Border>
<Border.Effect>
<DropShadowEffect Direction="-75" ShadowDepth="2" BlurRadius="5" />
</Border.Effect>
<Grid Background="White" Margin="5" ContextMenu="{StaticResource Menu}">
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="{Binding ProcessName}" Margin="0,0,5,0" />
<TextBlock Text="{Binding Percentage}" />
<TextBlock Text="%" />
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>
30 changes: 30 additions & 0 deletions src/Hitomi/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Windows;
using System.Windows.Input;

namespace Hitomi
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}

private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}

private void MainWindow_OnDeactivated(object sender, EventArgs e)
{
((Window) sender).Topmost = true;
}
}
}
Loading

0 comments on commit 918b211

Please sign in to comment.