Skip to content

Commit

Permalink
feat: add shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 5, 2021
1 parent 01b53ef commit c1c7dd2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .changes/shadows.md
@@ -0,0 +1,4 @@
---
"win7-notifications": minor
---
Add shadows
1 change: 1 addition & 0 deletions build.rs
Expand Up @@ -8,6 +8,7 @@ fn main() {
Windows::Win32::System::LibraryLoader::{GetModuleHandleW},
Windows::Win32::System::Com::{CoCreateInstance, CoInitialize},
Windows::Win32::Graphics::Gdi::*,
Windows::Win32::Graphics::Dwm::*,
Windows::Win32::UI::Shell::{ITaskbarList, TaskbarList},
Windows::Win32::UI::Controls::*,
Windows::Win32::UI::WindowsAndMessaging::*,
Expand Down
44 changes: 27 additions & 17 deletions src/notification.rs
Expand Up @@ -9,7 +9,8 @@ use std::{

use crate::Windows::Win32::{
Foundation as w32f,
Graphics::Gdi as w32gdi,
Graphics::Dwm,
Graphics::Gdi,
System::LibraryLoader,
UI::{Controls, WindowsAndMessaging as w32wm},
};
Expand Down Expand Up @@ -114,7 +115,7 @@ impl Notification {
lpfnWndProc: Some(window_proc),
lpszClassName: w32f::PWSTR(class_name.as_mut_ptr() as _),
hInstance: hinstance,
hbrBackground: w32gdi::CreateSolidBrush(WND_CLR.to_int()),
hbrBackground: Gdi::CreateSolidBrush(WND_CLR.to_int()),
..Default::default()
};
w32wm::RegisterClassW(&wnd_class);
Expand Down Expand Up @@ -151,6 +152,15 @@ impl Notification {
return Err(windows::Error::from_win32());
}

// shadows
let margins = Controls::MARGINS {
cxLeftWidth: 1,
cxRightWidth: 0,
cyBottomHeight: 0,
cyTopHeight: 0,
};
Dwm::DwmExtendFrameIntoClientArea(hwnd, &margins)?;

util::skip_taskbar(hwnd)?;
w32wm::ShowWindow(hwnd, w32wm::SW_SHOWDEFAULT);

Expand Down Expand Up @@ -278,7 +288,7 @@ pub unsafe extern "system" fn window_proc(

if lparam.0 == (*userdata).close_btn.0 {
// change the close button control background color to match the window color
return w32f::LRESULT(w32gdi::CreateSolidBrush(WND_CLR.to_int()).0 as _);
return w32f::LRESULT(Gdi::CreateSolidBrush(WND_CLR.to_int()).0 as _);
}

w32wm::DefWindowProcW(hwnd, msg, wparam, lparam)
Expand All @@ -288,19 +298,19 @@ pub unsafe extern "system" fn window_proc(
let userdata = userdata as *mut WindowData;
let lpds = lparam.0 as *mut Controls::DRAWITEMSTRUCT;

w32gdi::SetBkMode((*lpds).hDC, w32gdi::TRANSPARENT);
Gdi::SetBkMode((*lpds).hDC, Gdi::TRANSPARENT);

// draw notification close button
if (*lpds).hwndItem == (*userdata).close_btn {
w32gdi::SetTextColor((*lpds).hDC, Color(150, 150, 150).to_int());
w32gdi::TextOutW((*lpds).hDC, 5, 1, "x", 1);
Gdi::SetTextColor((*lpds).hDC, Color(150, 150, 150).to_int());
Gdi::TextOutW((*lpds).hDC, 5, 1, "x", 1);
}

// draw notification app name
if (*lpds).hwndItem == (*userdata).appname_control {
util::set_font((*lpds).hDC, "Segeo UI", 15, 400);
w32gdi::SetTextColor((*lpds).hDC, TITILE_CLR.to_int());
w32gdi::TextOutW(
Gdi::SetTextColor((*lpds).hDC, TITILE_CLR.to_int());
Gdi::TextOutW(
(*lpds).hDC,
5,
1,
Expand All @@ -312,8 +322,8 @@ pub unsafe extern "system" fn window_proc(
// draw notification summary (title)
if (*lpds).hwndItem == (*userdata).summary_control {
util::set_font((*lpds).hDC, "Segeo UI", 18, 700);
w32gdi::SetTextColor((*lpds).hDC, TITILE_CLR.to_int());
w32gdi::TextOutW(
Gdi::SetTextColor((*lpds).hDC, TITILE_CLR.to_int());
Gdi::TextOutW(
(*lpds).hDC,
0,
0,
Expand All @@ -325,15 +335,15 @@ pub unsafe extern "system" fn window_proc(
// draw notification body
if (*lpds).hwndItem == (*userdata).body_control {
util::set_font((*lpds).hDC, "Segeo UI", 18, 400);
w32gdi::SetTextColor((*lpds).hDC, SUBTITLE_CLR.to_int());
Gdi::SetTextColor((*lpds).hDC, SUBTITLE_CLR.to_int());
let mut rc = w32f::RECT::default();
w32wm::GetClientRect((*lpds).hwndItem, &mut rc);
w32gdi::DrawTextW(
Gdi::DrawTextW(
(*lpds).hDC,
(*userdata).notification.body.clone(),
(*userdata).notification.body.len() as _,
&mut rc,
w32gdi::DT_LEFT | w32gdi::DT_EXTERNALLEADING | w32gdi::DT_WORDBREAK,
Gdi::DT_LEFT | Gdi::DT_EXTERNALLEADING | Gdi::DT_WORDBREAK,
);
}

Expand All @@ -353,8 +363,8 @@ pub unsafe extern "system" fn window_proc(

// draw notification icon
if let Some(hicon) = util::get_hicon_from_buffer(&(*userdata).notification.icon, 16, 16) {
let mut ps = w32gdi::PAINTSTRUCT::default();
let hdc = w32gdi::BeginPaint(hwnd, &mut ps);
let mut ps = Gdi::PAINTSTRUCT::default();
let hdc = Gdi::BeginPaint(hwnd, &mut ps);
w32wm::DrawIconEx(
hdc,
NOTI_M,
Expand All @@ -363,10 +373,10 @@ pub unsafe extern "system" fn window_proc(
NOTI_ICON_S,
NOTI_ICON_S,
0,
w32gdi::HBRUSH::default(),
Gdi::HBRUSH::default(),
w32wm::DI_NORMAL,
);
w32gdi::EndPaint(hwnd, &ps);
Gdi::EndPaint(hwnd, &ps);
}

w32wm::DefWindowProcW(hwnd, msg, wparam, lparam)
Expand Down
32 changes: 16 additions & 16 deletions src/util.rs
Expand Up @@ -4,7 +4,7 @@

use crate::Windows::Win32::{
Foundation as w32f,
Graphics::Gdi as w32gdi,
Graphics::Gdi,
System::Com,
UI::{Controls, Shell, WindowsAndMessaging as w32wm},
};
Expand Down Expand Up @@ -62,18 +62,18 @@ pub fn get_loword(dword: u32) -> u16 {
(dword & 0xFFFF) as u16
}

pub unsafe fn primary_monitor() -> w32gdi::HMONITOR {
pub unsafe fn primary_monitor() -> Gdi::HMONITOR {
let pt = w32f::POINT { x: 0, y: 0 };
w32gdi::MonitorFromPoint(&pt, w32gdi::MONITOR_DEFAULTTOPRIMARY)
Gdi::MonitorFromPoint(&pt, Gdi::MONITOR_DEFAULTTOPRIMARY)
}

pub unsafe fn get_monitor_info(hmonitor: w32gdi::HMONITOR) -> w32gdi::MONITORINFOEXW {
let mut monitor_info = w32gdi::MONITORINFOEXW::default();
pub unsafe fn get_monitor_info(hmonitor: Gdi::HMONITOR) -> Gdi::MONITORINFOEXW {
let mut monitor_info = Gdi::MONITORINFOEXW::default();
monitor_info.__AnonymousBase_winuser_L13558_C43.cbSize =
std::mem::size_of::<w32gdi::MONITORINFOEXW>() as u32;
w32gdi::GetMonitorInfoW(
std::mem::size_of::<Gdi::MONITORINFOEXW>() as u32;
Gdi::GetMonitorInfoW(
hmonitor,
&mut monitor_info as *mut w32gdi::MONITORINFOEXW as *mut w32gdi::MONITORINFO,
&mut monitor_info as *mut Gdi::MONITORINFOEXW as *mut Gdi::MONITORINFO,
);
monitor_info
}
Expand All @@ -86,8 +86,8 @@ pub unsafe fn skip_taskbar(hwnd: w32f::HWND) -> Result<()> {
Ok(())
}

pub unsafe fn set_font(hdc: w32gdi::HDC, name: &str, size: i32, weight: i32) {
let hfont = w32gdi::CreateFontW(
pub unsafe fn set_font(hdc: Gdi::HDC, name: &str, size: i32, weight: i32) {
let hfont = Gdi::CreateFontW(
size,
0,
0,
Expand All @@ -96,14 +96,14 @@ pub unsafe fn set_font(hdc: w32gdi::HDC, name: &str, size: i32, weight: i32) {
false.into(),
false.into(),
false.into(),
w32gdi::DEFAULT_CHARSET,
w32gdi::OUT_DEFAULT_PRECIS,
w32gdi::CLIP_DEFAULT_PRECIS,
w32gdi::CLEARTYPE_QUALITY,
w32gdi::FF_DONTCARE,
Gdi::DEFAULT_CHARSET,
Gdi::OUT_DEFAULT_PRECIS,
Gdi::CLIP_DEFAULT_PRECIS,
Gdi::CLEARTYPE_QUALITY,
Gdi::FF_DONTCARE,
name,
);
w32gdi::SelectObject(hdc, hfont);
Gdi::SelectObject(hdc, hfont);
}

pub fn get_hicon_from_buffer(buffer: &[u8], width: i32, height: i32) -> Option<w32wm::HICON> {
Expand Down

0 comments on commit c1c7dd2

Please sign in to comment.