Skip to content

Add focus fix #3251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkgs/dartpad_ui/lib/blur.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.

import 'dart:js_interop';

import 'package:web/web.dart';

const _flutterViewSelector = 'flutter-view';

void addBlurListener() {
window.addEventListener('blur', _onBlur.toJS);
}

void removeBlurListener() {
window.removeEventListener('blur', _onBlur.toJS);
}

void _onBlur(Event _) {
print('onBlur');
final activeElement = document.activeElement as HTMLElement?;
if (activeElement == null) return;
// Only call blur on elements that are within a Flutter view.
final inFlutterView = activeElement.closest(_flutterViewSelector) != null;
print('inFlutterView = $inFlutterView');
if (inFlutterView) {
print('activeElement = $activeElement');
activeElement.blur();
}
}
4 changes: 4 additions & 0 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:split_view/split_view.dart';
import 'package:url_launcher/url_launcher.dart' as url_launcher;
import 'package:vtable/vtable.dart';

import 'blur.dart';
import 'console.dart';
import 'embed.dart';
import 'enable_gen_ai.dart';
Expand Down Expand Up @@ -301,6 +302,9 @@ class _DartPadMainPageState extends State<DartPadMainPage>
if (widget.runOnLoad) {
appServices.performCompileAndRun();
}
})
.then((_) {
addBlurListener();
});
appModel.compilingState.addListener(_handleRunStarted);

Expand Down
31 changes: 31 additions & 0 deletions pkgs/dartpad_ui/web/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,34 @@ window.addEventListener('load', function () {
window.addEventListener('message', messageHandler, false);
parent.postMessage({ 'sender': 'frame', 'type': 'ready' }, '*');
});

window.addEventListener('blur', onBlurHandler);

// Blur listener
const flutterViewSelector = 'flutter-view';
function onBlurHandler(event) {
console.log('onBlur');
const activeElement = document.activeElement;
if (!activeElement) {
return;
}
if (!(activeElement instanceof HTMLElement) || typeof activeElement.closest !== 'function') {
return;
}
const inFlutterView = activeElement.closest(flutterViewSelector) !== null;
console.log(`JS inFlutterView = ${inFlutterView}`);

if (inFlutterView) {
console.log('JS activeElement = ', activeElement);
activeElement.blur();
}
}

function addBlurListener() {
window.addEventListener('blur', onBlurHandler);
console.log('JS Blur listener added.');
}
function removeBlurListener() {
window.removeEventListener('blur', onBlurHandler);
console.log('JS Blur listener removed.');
}