Here's the relevant code (Godot 3.5.2-rc1):
extends AcceptDialog
...
onready var viewport_rect: Rect2 = get_viewport_rect()
...
#warning-ignore:UNUSED_ARGUMENT
func _process(delta: float) -> void:
# Prevent dialog from being dragged out of viewport.
var rect: Rect2 = get_rect()
# Check first if window is enclosed by viewport. The built-in method
# encloses() is theoretically faster than GDScript, and the condition will
# almost always be true, so it's worthwhile to do this check.
if viewport_rect.encloses(rect):
return
# Window is not enclosed by the viewport. Adjust window's position minimally
# so that window is enclosed by the viewport.
# Note that position must be set by changing the property rect_position, not
# the local variable rect.
if rect.position.x < viewport_rect.position.x:
rect_position.x = viewport_rect.position.x
elif rect.end.x > viewport_rect.end.x:
rect_position.x = viewport_rect.end.x - rect.size.x
if rect.position.y < viewport_rect.position.y:
rect_position.y = viewport_rect.position.y
elif rect.end.y > viewport_rect.end.y:
rect_position.y = viewport_rect.end.y - rect.size.y