mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
add hosts editor doc
This commit is contained in:
BIN
doc/devdocs/images/hostsfileeditor/code structure.png
Normal file
BIN
doc/devdocs/images/hostsfileeditor/code structure.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
102
doc/devdocs/modules/hostsfileeditor.md
Normal file
102
doc/devdocs/modules/hostsfileeditor.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# Hosts File Editor
|
||||
|
||||
[Learn](https://learn.microsoft.com/en-us/windows/powertoys/hosts-file-editor)
|
||||
|
||||
## Overview
|
||||
|
||||
The Hosts File Editor module provides a convenient way to edit the system's hosts file. The hosts file is a plain text file used by the operating system to map hostnames to IP addresses, allowing users to override DNS for specific domain names.
|
||||
|
||||
## Code Structure
|
||||
|
||||

|
||||
|
||||
The Hosts File Editor module is structured into three primary components:
|
||||
|
||||
1. **Hosts** - Entry point for the Hosts File Editor. Manages core services and settings through helper utilities.
|
||||
2. **HostsModuleInterface** - Interface for integrating the Hosts module with the PowerToys system.
|
||||
3. **HostsUILib** - Implements the UI layer using WinUI 3.
|
||||
|
||||
This structure is similar to the Environment Variables for Windows module.
|
||||
|
||||
## Key Components
|
||||
|
||||
### Main Entry Points
|
||||
|
||||
- **Module Entry**: [Program.cs](/src/modules/Hosts/Program.cs) → [App.xaml.cs](/src/modules/Hosts/HostsXAML/App.xaml.cs)
|
||||
- **Settings UI**:
|
||||
- Main Window: [MainWindow.xaml.cs](/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml.cs)
|
||||
- View: [HostsMainPage.xaml.cs](/src/modules/Hosts/HostsUILib/HostsMainPage.xaml.cs)
|
||||
- ViewModel: [HostsMainPage.xaml.cs](/src/modules/Hosts/HostsUILib/HostsMainPage.xaml.cs)
|
||||
- **Runner Integration**: [HostsModuleInterface](/src/modules/Hosts/HostsModuleInterface)
|
||||
|
||||
### Runner Integration
|
||||
|
||||
The module is loaded by the PowerToys runner from:
|
||||
- [main.cpp](/src/runner/main.cpp) (Lines 183-184): Loads Hosts Module using `L"WinUI3Apps/PowerToys.HostsModuleInterface.dll"`
|
||||
|
||||
### Settings Management
|
||||
|
||||
- [HostsViewModel.cs](/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs): Hosts UI in PowerToys settings
|
||||
- [HostsProperties.cs](/src/settings-ui/Settings.UI.Library/HostsProperties.cs): In settings UI
|
||||
- [HostsSettings.cs](/src/settings-ui/Settings.UI.Library/HostsSettings.cs): Wrapper with HostsProperties
|
||||
|
||||
### Module Components
|
||||
|
||||
#### HostsModuleInterface
|
||||
|
||||
- Defines the interface for integrating the Hosts module with the PowerToys system.
|
||||
|
||||
#### Hosts (Main Project)
|
||||
|
||||
- [Program.cs](/src/modules/Hosts/Hosts/Program.cs): Launch app
|
||||
- [HostsXAML](/src/modules/Hosts/Hosts/HostsXAML): Initialize service and loads the main window
|
||||
- [Host.cs](/src/modules/Hosts/Hosts/Helpers/Host.cs): Access to services register
|
||||
- [NativeEventWaiter.cs](/src/modules/Hosts/Hosts/Helpers/NativeEventWaiter.cs): Gets the dispatcher queue for posting UI updates from a background thread
|
||||
- [UserSettings.cs](/src/modules/Hosts/Hosts/Settings/UserSettings.cs): Manages reading, tracking, and updating user settings from settings.json
|
||||
|
||||
#### HostsUILib
|
||||
|
||||
- [HostsMainPage.xaml.cs](/src/modules/Hosts/HostsUILib/HostsMainPage.xaml.cs): Main page
|
||||
- [ViewModels](/src/modules/Hosts/HostsUILib/ViewModels): Contains view models that manage state and logic
|
||||
- [Models](/src/modules/Hosts/HostsUILib/Models): Models for managing host entries
|
||||
- [AddressType.cs](/src/modules/Hosts/HostsUILib/Models/AddressType.cs): Specifies whether an address is IPv4, IPv6, or Invalid
|
||||
- [Entry.cs](/src/modules/Hosts/HostsUILib/Models/Entry.cs): Represents a single hosts file entry (IP address, hostnames, comment, flags)
|
||||
- [HostsData.cs](/src/modules/Hosts/HostsUILib/Models/HostsData.cs): Converts the list of entries into a read-only collection
|
||||
- [Settings](/src/modules/Hosts/HostsUILib/Settings): Settings configuration
|
||||
- [Consts.cs](/src/modules/Hosts/HostsUILib/Consts.cs): Defines constants like max hosts IP length
|
||||
- [Helpers](/src/modules/Hosts/HostsUILib/Helpers): Utilities for dealing with hosts IP, filter features, and file loading
|
||||
|
||||
## Call Flow
|
||||
|
||||
1. **Enable app**: runner/main.cpp → settings.ui/settings.ui.library
|
||||
2. **Start app**: Program.cs → HostsXAML → HostsMainPage
|
||||
3. **Load hosts data**: ViewModel → HostsData → Helpers (load and parse file)
|
||||
4. **User edits**: UI bound to ViewModel updates entries
|
||||
5. **Save changes**: ViewModel triggers file write through Helpers
|
||||
6. **Settings management**: UserSettings.cs persists user preferences
|
||||
|
||||
## Key Features
|
||||
|
||||
| Feature | Key Function |
|
||||
|---------|--------------|
|
||||
| Adding a new entry | `Add(Entry entry)` |
|
||||
| Filtering host file entries | `ApplyFilters()` |
|
||||
| Open Hosts File | `ReadHosts()` |
|
||||
| Additional Lines | `UpdateAdditionalLines(string lines)` |
|
||||
|
||||
## Settings
|
||||
|
||||
| Setting | Implementation |
|
||||
|---------|---------------|
|
||||
| Open as administrator | `UserSettings()` |
|
||||
| Additional lines position | `UserSettings()->AdditionalLinesPosition` |
|
||||
| Consider loopback addresses as duplicates | `UserSettings()->LoopbackDuplicates` |
|
||||
| Encoding Setting | `UserSettings()->Encoding` |
|
||||
|
||||
## How to Build and Debug
|
||||
|
||||
1. Build PowerToys Project in debug mode
|
||||
2. Set Hosts as the startup project
|
||||
3. Launch Hosts File Editor in debug mode
|
||||
4. Attach the debugger to PowerToys.Hosts.dll
|
||||
5. Add breakpoints in the Hosts code
|
||||
@@ -8,6 +8,7 @@ This section contains documentation for individual PowerToys modules, including
|
||||
|--------|-------------|---------------|
|
||||
| Environment Variables | Tool for managing user and system environment variables | [Architecture & Implementation](environmentvariables.md) |
|
||||
| FancyZones | Window manager utility for custom window layouts | [Architecture & Implementation](fancyzones.md), [Debugging Tools](fancyzones-tools.md) |
|
||||
| Hosts File Editor | Tool for managing the system hosts file | [Architecture & Implementation](hostsfileeditor.md) |
|
||||
| Keyboard Manager | Tool for remapping keys and keyboard shortcuts | [Documentation](keyboardmanager/README.md) |
|
||||
| NewPlus | Context menu extension for creating new files in File Explorer | [Architecture & Implementation](newplus.md) |
|
||||
| Quick Accent | Tool for quickly inserting accented characters and special symbols | [Architecture & Implementation](quickaccent.md) |
|
||||
|
||||
Reference in New Issue
Block a user