Skip to content

Conversation

@ThomasMonkman
Copy link

Hello! First off this has been a great library to use, thank you very much for making it.

I've added the ability to set the brightness levels based off the ambient light levels from an yocto light sensor

It works exactly like phones or laptops where it will just adjust the light levels based on how bright the room is.

You set a brightness mapping per screen, setting the 0% and 100% brightness at "X" Lux, it then linearly interpolates between that based on sensor readings.

I've been using it for the last few weeks, and it's like automagic :) as I found my self always having to adjust my screens, especially now its getting dark so fast in winter.

It can be toggled per screen, as well light levels set per screen.

Translations

I've completely left out translations as I wasn't sure how new translations are integrated, I do use a fair amount of information inside strings, so I'm not sure how that would work. But I'm happy to add that in.

I've tired my best to make minimal changes to the repo, and kept in the style you currently have everything.

Settings

I've also tried to make the settings ui as informative as possible, so hopefully someone can set it up without help

User Flow

  1. install yocto virtual hub service on pc
  2. Set password and user name in browser using the virtual hub ui
  3. plug in sensor
  4. enter connection password and username in the settings
  5. adjust brightness of screens to match what you want

Error handling

I've added error handling as much as I could, here are some screenshots show that off

Everything ok
yocto settings

No sensor found
yocto sensor not found
No connection to virtual hub
yocto virtual hub not found

using an yocto light sensor to automatically set the screen brightness based off the ambient light level in lux

it can be toggled per screen, as well light levels set per screen.
@xanderfrangos
Copy link
Owner

Very cool feature! Thanks for the PR!

Light sensor support has come up many times over the years, but the sensor requested hasn't been consistent. If Twinkle Tray were to offer light sensor support, I would like for it to be as vendor agnostic as possible. For example, I would prefer the settings tab be called "Light Sensor", with an on-page option to select the type of sensor the user has (if supported). From there, users could input the settings relevant to their light sensor type.

I don't have a light sensor myself, so I would love to hear what other users have to say about this.

I'm not going to merge this PR as-is, but I have activated the workflow so that users can play around with the feature:
https://github.com/xanderfrangos/twinkle-tray/actions/runs/19647041391

@xanderfrangos
Copy link
Owner

Just to clarify, I don't expect you to add support for other light sensors. The idea is that if/when support for other light sensors are contributed, they can share the same settings tab/UI.

@ThomasMonkman
Copy link
Author

Ok, thanks I'm going to go ahead and make those changes, hopefully before Christmas.

I'll change it so it's more extendable, although of course it will be a bit of guess work as I don't know how other sensors will work.

@psxvoid
Copy link

psxvoid commented Dec 11, 2025

Hi,

@xanderfrangos Were there any attempts to integrate laptops sensors? Many laptops have ambient light sensors, including mine.

I was thinking that when a laptop has a built-in ambient light sensor and it's connected to an external monitor, it can control it's brightness automatically.

@ThomasMonkman
Copy link
Author

Hi @psxvoid, So while revisiting the pr, I've looked in to the ambient sensors built in to a laptop, as i figured how hard can it be?

What I have found is there is a simple windows UWP api for it.
https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.lightsensor?view=winrt-26100

however that is an UWP api, and im not really sure how to consume that in to electron, I've tired edge-js, but that api isn't avabile that way (although i didn't spend much time on it)

here was the code I quickly tired (just make a bare bones node project with edge-js installed and run this as a script)

var edge = require('edge-js');

var getLight = edge.func({
    source: function() {/*
        using System.Threading.Tasks;
        using Windows.Devices.Sensors;

        public class Startup
        {
            public async Task<object> Invoke(dynamic input)
            {
                var sensor = LightSensor.GetDefault();
                if (sensor == null)
                {
                    return "No light sensor available";
                }

                var reading = sensor.GetCurrentReading();
                return reading.IlluminanceInLux;
            }
        }
    */}
});

getLight(null, function (error, result) {
    if (error) throw error;
    console.log("Light sensor output (lux):", result);
});

another option is nodeRT but I found that requires installing visual studio 2013, although there are many versions of the package built for different version of windows sdk here
and im not sure how that versioning is handled, can you just use the oldest? or do you have to ship multiple versions? I assume the oldest works forwards. (but then does the ci need vs2013 installed??)

What I am going to do is add in a fake ui sensor, that lets you mimic having a sensor plugged in.

This also lets me test what its like to extend the sensor page with multiple sensors.

I will focus on the changes requested (more generic ui, support for multiple sensors in theory), then maybe look at how to add the ambient light sensor.

@psxvoid
Copy link

psxvoid commented Dec 11, 2025

Hi @ThomasMonkman,

I've seen this UWP API as well, though I was planning to use an older Win32 Sensor API, seems like it can be consumed similarly to how it's already done in other native modules in this repo. But nodeRT might be a better option indeed, haven't tried it.

I will focus on the changes requested (more generic ui, support for multiple sensors in theory), then maybe look at how to add the ambient light sensor.

Yes, sure, don't want to put any pressure on you :)

I was thinking that your feature might intersect a bit with what I want, but I'm not completely sure. Seems like your feature request is related to a remote light sensor - there is "yokto virtual hub" on your screenshots. And probably a UI for a built-in into a laptop ambient light sensor might be slightly different.

@ThomasMonkman
Copy link
Author

@psxvoid Thanks, ohh yes that looks like it could work.

So from what I've seen the ambient light sensor returns Lux, which is the same as the yocto sensor, so hopefully it should all just work "flawlessly" :D

haha they even have an article talking through light aware ui

I'm going to change the ui, so it should be simple to slot in another sensor.

and yes it would be great if it worked with the ambient light sensor of a laptop, i went down the yocto route as I wanted it on my pc.

@xanderfrangos
Copy link
Owner

@ThomasMonkman It's a real pain in the ass, but you can access the UWP APIs through C++. I had to write a few Node modules for Twinkle Tray that do this. For example:
https://github.com/xanderfrangos/twinkle-tray/blob/master/src/modules/tt-windows-utils/windows_media_status.cc
and
https://github.com/xanderfrangos/twinkle-tray/blob/master/src/modules/tt-windows-utils/windows_app_startup.cc

@ThomasMonkman
Copy link
Author

@xanderfrangos @psxvoid Ok I've got windows ambient light sensor working in the project using the old COM api you linked above, I need some time to wrap up the ui for it, but I think it shouldn't be much more work.

Then hopefully this should work for a wide range of people :)

I'm not sure if I should move this pr in to draft? But I need to push some in progress as I'm travelling and don't want it to disappear in to the ether. But it is not ready to review, I will ping when it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants